Home arrow static arrow Java Programming [Archive] - extracting an item from a vector
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - extracting an item from a vector
This topic has 5 replies on 1 page.

Posts:8
Registered: 7/9/04
extracting an item from a vector  
Jul 9, 2004 1:04 PM



 
Hi,

I have a vector of ints and I'm trying to extract them, but confused on what to do?

Private Vector Messages;

example of the vector
---------
int

----------|
int


.
.
.
---------
int

----------|

How can I get these int values from the vector?
 

Posts:3,258
Registered: 00-08-28
Re: extracting an item from a vector  
Jul 9, 2004 1:11 PM (reply 1 of 5)



 
Vectors are used to store objects not primitives. If however you are storing Integer objects in that case

you might use the following as an example. You would however have to change the code as per your req. Treat is ias a recipe not dinner

import java.util.Vector; public class VectorTest {     public static void main(String[] args)     {     Vector obj=new Vector();     for(int i=0;i<=100;i++)     {          obj.add("STudent number" +i);     }     for(int i=0;i<=100;i++)     {     System.out.println(obj.elementAt(i));     }         }  }  
 

Posts:147
Registered: 26.01.04
Re: extracting an item from a vector  
Jul 9, 2004 2:53 PM (reply 2 of 5)



 
Vector is a very slow datastructure. Its main purpose is when multiple threads work on it.
otherwise using an ArrayList or LinkedList should be better suited.
the ArrayList is dynamically expanding a normal array(you can also prereserve space), the LinkedList is linking objects in a chain-like manner.
hence, use ArrayList if u often extract the vectordata and LinkedList if u often add/remove/insert data.

u can access them via the implemented List-Interface(add(Object), get(index), size()...) The data can also be extracted via an Iterator(List.getIterator()) which is highly recommended when operating with LinkedLists.
 

Posts:1,183
Registered: 1/23/02
Re: extracting an item from a vector  
Jul 9, 2004 3:11 PM (reply 3 of 5)



 
Vector is a very slow datastructure

This is a very big exageration.
 

Posts:3,258
Registered: 00-08-28
Re: extracting an item from a vector  
Jul 9, 2004 3:33 PM (reply 4 of 5)



 
Vector is a very slow datastructure.

Vectors might be slower than ArrayList under certain conditions but I wouldnot say they are very slow.

[url http://www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html] a comparison between arraylist and vector [/url]
 

Posts:1,183
Registered: 1/23/02
Re: extracting an item from a vector  
Jul 9, 2004 4:05 PM (reply 5 of 5)



 
Vectors might be slower than ArrayList under certain conditions but I wouldnot say they are very slow.

In fact the comparion in Java Performace Tuning (p 291) shows that on 1.4.0 sever JVM Vector.set() performs better than ArayList.set().
 
This topic has 5 replies on 1 page.