Hello guys, I am new in Java and would like to have some advice on iterating and printing the contents of a vector.Below is a code of what I have done.
public class Test{ private String []names = {"Mary","kim"}; private Vector vec ;
public Test(){ vec = new Vector(); vec.add(names); for(int i=0;i <= vec.size();i++){ //I need to print the contents of vec System.out.println(""); } } public static void main( String[ ] args ){ new Test();
} }
pascal betz
Ranch Hand
Joined: Jun 19, 2001
Posts: 547
posted
0
Hi
first: you might want to look at the java.util.List and (one of the implementations java.util.ArrayList); unless required you should use these classes instead of Vector (Vector is synchronized, ArrayList is not. Butboth iomplement the List itnerface).
But your problem still stays :-) Iterating over a List is possible in two ways: - use the index - use the iterator
if you check out the API doc of List you will find the methods to get elements out of a List using the index of the item.
also in the API you will find the Iterator interface which can also be used to iterate over a List.
btw: in your example code there is just one element in your Vector: a String[] (and i assume you want to have two items beeing the two names)
once you've removed the element, try to output it with System.out.println.
Feel free to reply if you still have problems.
/Svend Rost
MaryT Tsele
Greenhorn
Joined: Oct 30, 2006
Posts: 8
posted
0
Originally posted by pascal betz: Hi
first: you might want to look at the java.util.List and (one of the implementations java.util.ArrayList); unless required you should use these classes instead of Vector (Vector is synchronized, ArrayList is not. Butboth iomplement the List itnerface).
But your problem still stays :-) Iterating over a List is possible in two ways: - use the index - use the iterator
if you check out the API doc of List you will find the methods to get elements out of a List using the index of the item.
also in the API you will find the Iterator interface which can also be used to iterate over a List.
btw: in your example code there is just one element in your Vector: a String[] (and i assume you want to have two items beeing the two names)
lets see if this gets you further....
pascal
Thank you Pascal and Svend I tried using the ArrayList, it worked fine, but I am not sure why I put the line Object names = (Object) iter.next(); in the for loop. It sort of popped out, and I decided to put Object and the code worked, please explain. And I also did the vector one, it also works but throws an exception after printing the contents. please help.
public class Test{ private String name1 = "kim"; private String name2 = "Mary"; private Vector vec ;
public Test(){ vec = new Vector(); vec.addElement(name1); vec.addElement(name2);
for(int i=0;i <=vec.size();i++){ System.out.println(vec.elementAt(i)); } } public static void main( String[ ] args ){ new Test();
} } throws this exception: java.lang.ArrayIndexOutOfBoundsException: 2 >= 2 at java.util.Vector.elementAt(Unknown Source) at Test.<init>(Test.java:15) at Test.main(Test.java:19) kim Mary Exception in thread "main"
sven studde
Ranch Hand
Joined: Sep 26, 2006
Posts: 148
posted
0
I tried using the ArrayList, it worked fine, but I am not sure why I put the line Object names = (Object) iter.next(); in the for loop.
1) First, you can only store objects in an ArrayList, Vector, etc. (So, if you want to store int's in an ArrayList you must covert them into Integer objects first.)
2) Any object you add to an ArrayList, Vector, etc., is automatically converted to type Object. (Object is a base class for all objects in Java.)
3) When you retrieve an object from an ArrayList, Vector, etc. you get an Object type back. Therefore, you need to cast the Object to the proper type.
I am not sure why I put the line Object names = (Object) iter.next(); in the for loop.
Either am I. Here is what you should be doing:
String aName = (String) iter.next();
That code casts the Object type that is returned from your ArrayList to type String.
And I also did the vector one, it also works but throws an exception after printing the contents. please help.
Maybe looking at the problem in the context of something you know better, like an array, will help you see the problem. Can you tell what the problem is with this code:
If you can't see the error right away, then write the code for how you would output the values in the array by hand--in other words without using a for-loop, and get that code to work.
Next, on a piece of paper write down the column headings: 'i', 'nums[i]', and 'value'. Then write down what happens when the for-loop executes. Under the column heading 'i', write down the value of 'i' when the loop starts. Then fill in the value for i in num[i], and write that under the column heading 'num[i]'. Finally, write down the corresponding value in the array under the column heading 'value'. Here is what the first line will look like:
Then increment i at the end of the loop, and go back to the top of the loop and test the if condition to see if you should continue with the loop. If you continue with the loop, write down the values under each column heading again. Do that until the for loop ends. When you are done, examine what you wrote under the column heading 'num[i]' and compare that to the code you wrote to output the array values by hand without using a for-loop.
[ November 03, 2006: Message edited by: sven studde ]
[ November 03, 2006: Message edited by: sven studde ]
[ November 03, 2006: Message edited by: sven studde ]
[ November 03, 2006: Message edited by: sven studde ]
[ November 03, 2006: Message edited by: sven studde ] [ November 03, 2006: Message edited by: sven studde ]