Vector of Arrays.

Hello,

I am looking to create a Vector of Arrays.

I am reading a comma delimited config file that has multiple lines of info. Each line would go into an element of the vector as a string array slit around the commas. Is this an easy thing to do? Would it be easier or harder to have a vector of vectors?

Thanks,

Marcus

[349 byte] By [Mag748a] at [2007-11-26 18:54:41]
# 1

> I am looking to create a Vector of Arrays.

> I am reading a comma delimited config file that has

> multiple lines of info. Each line would go into an

> element of the vector as a string array slit around

> the commas. Is this an easy thing to do?

Sure. Just create a new array and add it to the vector for each line.

> Would it be

> easier or harder to have a vector of vectors?

Depends on what you are going to do with the data structure once you create it. (For example if you are going to put it into a JTable then yes)

You might want to consider creating a new object for each line and having a Vector of objects that the data stands for. (Again it depends on what the data is and what you are using it for).

zadoka at 2007-7-9 20:32:11 > top of Java-index,Java Essentials,New To Java...
# 2

Ok, right now I have a loop that inserts the arrays into the vector. But when I try to read the array back. I get an "incompatable types" error. I think this is because the Vector will retrun an object when I am looking for a string array. How can I handle this?

The loop to create the vector is below:

String[] str = new String[50];

str = in.readLine().split(",");

while(str != null) {

friArray.addElement(str);

str = in.readLine().split(",");

}

And here I am trying to read it back:

public static void showFriFile() {

String[] str = new String[50];

for(int i = 0; i < friArray.size(); i++){

str = friArray.elementAt(i);

System.out.println(str[0]);

}

}

Thank you very much,

Marcus

Mag748a at 2007-7-9 20:32:11 > top of Java-index,Java Essentials,New To Java...
# 3

> Ok, right now I have a loop that inserts the arrays

> into the vector. But when I try to read the array

> back. I get an "incompatable types" error. I think

> this is because the Vector will retrun an object when

> I am looking for a string array.

Yes.

> How can I handle

> this?

You could cast. Or a better option would be to use generics to specify the class of the items in the Vector and use the enhanced for loop to loop over them. (You are using at least Java 1.5+ right?)

zadoka at 2007-7-9 20:32:11 > top of Java-index,Java Essentials,New To Java...
# 4

Thanks for the help. I think that I was making it a little too complicated with the vector winthin the vector. I am just going to keep the whole string in each element of the vector. and whenever I need a specific word in the line I will use the split command to create a temp String array. It makes it easier I think. Ill just keep to the basic for now.

Thank you !!

-Marcus

Mag748a at 2007-7-9 20:32:11 > top of Java-index,Java Essentials,New To Java...