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]

> 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).
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
> 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?)