Article View: borland.public.jbuilder.java.language
Article #628Re: ArrayList
From: Kevin Dean [TeamB]
Date: Mon, 04 Apr 2005 15:25
Date: Mon, 04 Apr 2005 15:25
32 lines
1894 bytes
1894 bytes
Petar Popara wrote:
>
> Why this doesn't work:
>
> ArrayList l = new ArrayList();
> l.add(new String("aaa")));
> String[] s = l.toArray();
>
> JavaClassCast exception I got. What should I do?
The toArray() method returns Object[], not String[] (see the source
code of ArrayList for implementation details). If you want an array of
String, I suggest the following:
String[] s = (String[])l.toArray(new String[0]);
That will force ArrayList to build an array of the appropriate size of
type String[]. You then cast it back to the String[] type.
--
Kevin Dean [TeamB]
Dolphin Data Development Ltd.
http://www.datadevelopment.com/
NEW WHITEPAPERS
Team Development with JBuilder and Borland Enterprise Server
Securing Borland Enterprise Server
http://www.datadevelopment.com/papers/index.html
Please see Borland's newsgroup guidelines at
http://info.borland.com/newsgroups/guide.html
Technical Details
Message-ID:
<xn0e0lkj816f90z002-kdean@www.teamb.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<42512ba2@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,b5d68c9c70412a4d
X-Google-Attributes: gidf74b3,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Kevin Dean [TeamB]" <NkOdSePaAnM@datadevelopment.com>
Subject: Re: ArrayList
Newsgroups: borland.public.jbuilder.java.language
References: <42512ba2@newsgroups.borland.com>
Organization: Dolphin Data Development Ltd.
User-Agent: XanaNews/1.17.3.1
X-Face: ":yn4zt^A_dn>6~{YJmu^B\'4w)e:apPM\VC=Dvf&*tASxX9|BYczkFd$xr.$vNSZ[3Uobr
p]c4\+\l){!WQI.$]veP({HLd;CDYH?iN
Message-ID: <xn0e0lkj816f90z002-kdean@www.teamb.com>
X-Ref: newsgroups.borland.com ~XNS:00000146
NNTP-Posting-Host: 24.57.153.235
Date: 4 Apr 2005 07:25:43 -0800
X-Trace: newsgroups.borland.com 1112624743 24.57.153.235 (4 Apr 2005 07:25:43 -0800)
Lines: 32
X-Authenticated-User: kdean
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail