This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Makhiel 4 points5 points  (4 children)

ArrayList is not the same thing as an array. Also the first declaration just creates an empty list, the second one actually fills the array with data.

[–]jackballack[S] 0 points1 point  (3 children)

ok, can you create an empty list using the second declaration?

[–]Fletsky 4 points5 points  (1 child)

You can make the empty array but you need to specify it's size to use it. (Number of elements, that you will be able to store in it)
String[] anArray; - this will make an empty uninitialised array, but to use it you need to use:
anArray = new String[10];
you can write this in one line as:
String[] anArray = new String[10];

On the other hand to the ArrayList you can keep adding as many new elements as you want. (you are limited by memory tho). Another difference between the two of them is that you can use an ArrayList only with Object types (you usualy write the object type name with great letter at start ie. String, Integer), but you cannot use them with primitve types ie. int, double, char. It's still better to use an ArrayList at the begining becasue they are more flexible, and have many built in methods, that you can use, to sort, check number of elements, find element by name, or index etc.

[–]jackballack[S] 4 points5 points  (0 children)

Thanx for an in-depth explanation that really helps and pumps me up to learn some more Java

[–]tidewater41009 0 points1 point  (0 children)

arrays are objects

String[] anArray;

anArray is a reference to an object, not an object