what to do with arrays

Can I declare arrays without knowing how long it is going to be?

If so how,

I keep getting errors when I don't state a length for my array

I tought I could declare and use it like this:

String[] user_files;

for(i=0;i<xx;i++)

{

user_files=xxxxx

}>

[323 byte] By [ugie] at [2007-9-26 2:21:00]
# 1

The code

String[] user_files;

does not create an array, it creates a reference to an array.

This reference must be set to point to an array with code like this:

user_files = new String[10];

It is when the array is created that you must specify the length.

If you want a data structure like an array, but do not know the length when you create it, use an ArrayList instead:

List l = new ArrayList();

for (i = 0; i < xx; i++) {

l.add(new Object());

}

schapel at 2007-6-29 9:25:34 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2

Arrays in java have a fixed length.You can declare a variable to be a reference to an array, like:

String[] user_files;

But, you can't use the variable user_files until it gets assigned to a String array. One way is to create a new array:

user_files=new String[20];

Another way is to call a method that returns an array of Strings:

user_files=myfile.list();

where myfile is a reference to a File object and the list() method of File returns a String array.

atmguy at 2007-6-29 9:25:34 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
When I need an array, but will not know how big it will be until run-time, I simply use a HashTable. You may want to look into that instead.
joseph_mueller at 2007-6-29 9:25:34 > top of Java-index,Archived Forums,New To Java Technology Archive...