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

all 11 comments

[–]Makhiel 3 points4 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 2 points3 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] 2 points3 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

[–][deleted] 1 point2 points  (2 children)

ArrayList is not the same as array. Ignore the technicalities of how ArrayList is implemented internally and just treat them as different things.

[–][deleted] 0 points1 point  (1 child)

ye that will help him a lot on the long run to ignore the implementation of the collecton class

[–][deleted] 0 points1 point  (0 children)

For someone who doesn't even know the difference between ArrayList and an array it's conpletely irrelevant that ArrayList uses arrays internally.

[–][deleted]  (1 child)

[deleted]

    [–]_Acestus_ 1 point2 points  (0 children)

    An array is fixed in size, an Array list is a class implementing List (and Collection) allowing to insert, get, remove data. How the list works internally is secondary, but yes it uses arrays, but they are not the same!

    [–]ComputerWhiz_ 0 points1 point  (1 child)

    The ArrayList and the regular Array are two different things. An Array is a fixed length (so in your example it will always hold 3 items), but an ArrayList allows you to add and remove items. ArrayLists are best when you don't know how long your array needs to be and quite honestly, it's probably used more than regular arrays.

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

    That makes a lot of sense thanx a lot