This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Printing the contents of a Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing the contents of a Vector" Watch "Printing the contents of a Vector" New topic
Author

Printing the contents of a Vector

MaryT Tsele
Greenhorn

Joined: Oct 30, 2006
Posts: 8
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.

import java.util.Vector;

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
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
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Hi Mary,

how do you remove an element from a vector?

Try to look at: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html

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

Check code for Array List:
***************************************************************************
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class Test2 {
private String name1 = "kim";
private String name2 = "Mary";

public Test2(){
ArrayList array = new ArrayList();
array.add(name1);
array.add(name2);

for (Iterator iter = array.iterator(); iter.hasNext() {
Object names = (Object) iter.next();
System.out.println(names);
}

}
public static void main( String[ ] args ){
new Test2();

}
}
*************************************************************************
Check the coe for Vector:

import java.util.Vector;

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
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Printing the contents of a Vector