dynamic array

can we any way use any array with out giving it's length.i wanna declare a array and get input from std.in and store in a array. i do not know the size of the array before hand!!! -D.K
[219 byte] By [decaynan] at [2007-9-26 2:49:32]
# 1

In that case you should use eg. java.util.Vector or java.util.LinkedList. Arrays in java are static and their size must be defined on creation.

Adding an object to a Vector (as the last element: vector.add(object);

Getting an object from a Vector at the index i: obj = vector.getElementAt(i);

iterating through the vector:

Enumeration e = vector.getElements();

while (e.hasMoreElements()) System.out.println(e.nextElement());

Getting the elements as an array:

Object[] objs = vector.toArray();

...

jsalonen at 2007-6-29 10:35:33 > top of Java-index,Archived Forums,Java Programming...
# 2
I hope not,Vector is generally used for this purpose.That is growable array.Length must be known ,
pathreading at 2007-6-29 10:35:33 > top of Java-index,Archived Forums,Java Programming...
# 3

Check out the ArrayList class

http://java.sun.com/j2se/1.3/docs/api/java/util/ArrayList.html

> can we any way use any array with out giving it's

> length.

>

> i wanna declare a array and get input from std.in and

> store in a array. i do not know the size of the array

> before hand!!!

>

>

> -D.K

pbalaram at 2007-6-29 10:35:33 > top of Java-index,Archived Forums,Java Programming...