Article View: borland.public.jbuilder.java.language
Article #7721Re: Help with an array?
From: rp
Date: Thu, 31 May 2001 02:08
Date: Thu, 31 May 2001 02:08
94 lines
3801 bytes
3801 bytes
The way I would approch this is to create a class that will encapsulate one
record of your file data,
then just create object instances of this class as you read the data from
the file and add them to an ArrayList.
The table model can use the ArrayList directly for its data.
public Object getValueAt(int row, int col) {
YourObject record = (YourObject)modelData.get(row);
/*now switch on col and return the appropriate object member*/
}
If you still just want to get an array from a vector you can loop thru it.
String[] mystringArray = new String[myVector.size()];
for(int x=0; x<vec.size(); x++) {
mystringArray[x] = (String)myVector.get(x);
}
If you were using the Collection Framework ArrayList you could use toArray()
.
"Alex Colic" <alex.colic@pop-ware.com> wrote in message
news:3b13b906_2@dnews...
> Hi, I am trying to create my own model to populate a JTable. I have a
> deliminated text file. An example from the file is:
>
> 500005~Electrical~~0~0
> 500006~Electrician~~1~0.1
>
> I have created a class that reads the file and searchs on a criteria. The
> first column above is the criteria. I then populate a vector with the
> results. I then need to cast the vector to an array.
>
> My code to read the file and populate the vector is as follows:
>
> public Vector readFile() throws FileNotFoundException, IOException
> {
> int n;
> String s;
>
> Vector objects=new Vector();
> FileInputStream in=new FileInputStream(getFile());
>
> while((n=in.available())>0)
> {
> byte[] b=new byte[n];
> int result=in.read(b);
> if(result== -1) break;
> s=new String(b);
> String lineSep=System.getProperty("line.separator");
> StringTokenizer fileToken=new StringTokenizer(s,lineSep);
> StringTokenizer lineToken;
> while(fileToken.hasMoreTokens())
> {
> String nextLine=fileToken.nextToken();
> String[] row;
> if(nextLine.startsWith(searchCiteria))
> {
> lineToken=new StringTokenizer(nextLine, "~");
> row=new String[lineToken.countTokens()];
> int i=0;
> while(lineToken.hasMoreElements())
> {
> row[i]=lineToken.nextToken();
> i++;
> }
> objects.add(row);
> }
> }
> }
> return objects;
> }
>
> As you can see I have a vector 'objects' returned.
>
> My question is the how do I cast that vector to an array?
>
> My original array is as follows:
>
> String [][] theData ={
> {"this", "is", "a", "test"}, {"THIS",
> "IS", "A", "TEST"}
> };
>
> Sorry for the long question.
>
> Any help is appreciated.
>
> Alex
>
>
>
>
>
>
Technical Details
Message-ID:
<3b15a824$1_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!news.tele.dk!newsfeed2.skycache.com!newsfeed.skycache.com!Cidera!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnewsReferences:
<3b13b906_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,1d31711f10772d50 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-05-30 19:13:21 PST From: "rp" <admin@objectpascal.com> Newsgroups: borland.public.jbuilder.java.language References: <3b13b906_2@dnews> Subject: Re: Help with an array? Date: Wed, 30 May 2001 21:08:57 -0500 Lines: 96 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 NNTP-Posting-Host: 216.54.255.206 Message-ID: <3b15a824$1_1@dnews> X-Trace: dnews 991275044 216.54.255.206 (30 May 2001 19:10:44 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!news.tele.dk!newsfeed2.skycache.com!newsfeed.skycache.com!Cidera!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews