The Artima Developer Community
Sponsored Link

Java Answers Forum
How do i access Vector elements?

7 replies on 1 page. Most recent reply: Jul 19, 2002 11:43 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

How do i access Vector elements? Posted: Jul 18, 2002 3:23 PM
Reply to this message Reply
Advertisement
Here is my question: How do I access the individual portions inside a vector element?
Here is what I did:

// locate city1 in Vector v
int city1FoundAt = V.indexOf (city1);
// retrieve element
Object city1Data = V.get (city1FoundAt);

Now I should have city1Data containing all the necessary details to do calculations, but how do I get at them? Lets say the vector element

index(0) = ALBERTA, Calgary, 51, 6, 114, 1

how do i get Calgary out of this?
or 51? 6? etc... please help.. thanks


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: How do i access Vector elements? Posted: Jul 18, 2002 3:48 PM
Reply to this message Reply
Instead of:
   [b]Object[/b] city = cityVector.get( index );
You should have:
   [b]City[/b] city = [b](City)[/b]cityVector.get( index );
Where City is the object stored in the Vector which has useful methods such as getName(), getProvince(), and getMeanlessUndocumentedNumbers().

Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

Re: How do i access Vector elements? Posted: Jul 18, 2002 6:34 PM
Reply to this message Reply
wait.. i dont get how i could access each of those individual details within the vector element

Saisantosh Polavarapu

Posts: 2
Nickname: sai
Registered: Jul, 2002

Re: How do i access Vector elements? Posted: Jul 19, 2002 5:06 AM
Reply to this message Reply
Hi,

Parse the Object your getting from the Vector element:

Say you have the following code
// ....
// .....
String str = v.get(indexOfCity);
StringTokenizer strToken = new StringTokenizer(str,",");
while(strToken.hasMoreElements()){
//... you get the token values here...
System.out.println(strToken.nextToken());
}
// .....
// ....

Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

Re: How do i access Vector elements? Posted: Jul 19, 2002 3:12 PM
Reply to this message Reply
ok here is my code to put the file in to vector V:

try
{
// load data into Vector V
Vector V = new Vector ();
BufferedReader inFile = new BufferedReader (new FileReader (filename));
String line = inFile.readLine ();
while ((line = inFile.readLine ()) != null)
{
V.addElement (line);
}
inFile.close ();
return V;
}
catch (IOException e)
{
System.out.println (e.getMessage ());
return null;
} //end try block


how do i use that string tokenizer to search the individual elements of vector V?

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: How do i access Vector elements? Posted: Jul 19, 2002 4:30 PM
Reply to this message Reply
Ok... so you are not loading any user-defined Object into the vector. As each elevemt of the vector is a string, follow the instructions given by Matt and cast the value obtained into a String.

After you get the string, StringTokenizer can be used.

Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

Re: How do i access Vector elements? Posted: Jul 19, 2002 7:19 PM
Reply to this message Reply
thanx i got it

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: How do i access Vector elements? Posted: Jul 19, 2002 11:43 PM
Reply to this message Reply
Two more points (which translates to about 2 cents worth):
1) It looks like you are losing the first line of the file.
2) There is no point in putting all the data in a Vector, if it is just lines from a file. You are only deferring the parsing to a subsequent loop through the Vector. You may as well parse the lines as soon as you get them; if you are creating some objects from the parsed data, it might be worthwhile to collect those objects in the Vector.

Flat View: This topic has 7 replies on 1 page
Topic: quick example please Previous Topic   Next Topic Topic: right click selection

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use