Article View: borland.public.jbuilder.java.language
Article #8707Re: Declaring an Array of Strings?
From: Marcel Gabi
Date: Thu, 08 Mar 2001 17:52
Date: Thu, 08 Mar 2001 17:52
57 lines
2625 bytes
2625 bytes
You have to assign the actual array first.
What you actually have now is only a reference
to a string array which has not yet been initialized.
You can initialize an array by doing the following:
String[] myArray = new String[size]; wher size is the
size of the array.
Or you can also fill up the array directly by doing
this:
String[] addressLine = {"0", "1", "2", "3", "4", "5", "6"};
Here is your code snippet again.
String[] addressLine = new String[7]; //the size is 7
for(int i=0; i<7; i++){
addressLine[i]= new String("xxx" + i);
}
//print out the content of the array
for(int j=0; j<7; j++){
System.err.println(addressLine[j]);
}
Regards,
Marcel
Thomas682 wrote:
> hi,
>
> String AddressLine[];
>
> for(int i=0;i<7;i++)
> AddressLine[i]= new String("xxx");
>
> no compile error but runtime exception-(Not exists)
>
> hmmmmm - Malloc( ?????
>
> Thanks for Help
>
> Thomas
>
> "Tor Iver Wilhelmsen" <tor.wilhelmsen@sds.no> wrote in message
> news:uu254bq17.fsf@sds.no...
> > "Harry Whitehouse" <harry@envmgr.com> writes:
> >
> > > String AddressLine[];
> >
> > Note that this only declares the reference, it does not assign an
> > actual array to it.
> >
> > > for(int i=0;i<7;i++)
> > > AddressLine[i]= new String; // line 208
> >
> > Unlike C++ and C#, constructors in Java need a parenthesis also when
> > there are no arguments to the constructor. Also, String doesn't have a
> > no-args constructor, so you need an argument.
Technical Details
Message-ID:
<3AA7C6C2.8F36FA7F@borland.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone-sf.pbi.net!63.208.208.143!feed2.onemain.com!feed1.onemain.com!newsfeed.icl.net!newsfeed00.sul.t-online.de!t-online.de!news1.dtag.de!news.stgl.sel.alcatel.de!news.stgl.sel.alcatel.de!bornews.borland.com!dnews!10.143.12.99References:
<3aa6df5f$1_2@dnews> <uu254bq17.fsf@sds.no> <3aa7a736_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,9324d780b1c839d2 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-08 10:10:10 PST Message-ID: <3AA7C6C2.8F36FA7F@borland.com> Date: Thu, 08 Mar 2001 09:52:02 -0800 From: Marcel Gabi <mgabi@borland.com> Organization: Borland Software Corporation X-Mailer: Mozilla 4.7 [en] (X11; I; SunOS 5.8 sun4u) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language Subject: Re: Declaring an Array of Strings? References: <3aa6df5f$1_2@dnews> <uu254bq17.fsf@sds.no> <3aa7a736_2@dnews> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 10.143.12.99 X-Trace: 8 Mar 2001 09:53:33 -0800, 10.143.12.99 Lines: 58 Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone-sf.pbi.net!63.208.208.143!feed2.onemain.com!feed1.onemain.com!newsfeed.icl.net!newsfeed00.sul.t-online.de!t-online.de!news1.dtag.de!news.stgl.sel.alcatel.de!news.stgl.sel.alcatel.de!bornews.borland.com!dnews!10.143.12.99