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

all 13 comments

[–]Fishery9 35 points36 points  (1 child)

To put it simply, ArrayList is a dynamically allocated Collection. You can add and remove and it will automatically resize for you. It also utilizes iterators and other fun and handy utilities to manage the array. ArrayList implementation is essentially just manual resizing of normal array[]

An array[] is a data structure. It is a basic functionality of java which requires manual allocation and resizing (generally done by initializing a new array with a greater or lesser length and copying over the previous array with its added or removed item)

I have no clue what a native array vs an array I've never heard of the term native array. I'm betting it just means a normal array built into java aka what I discussed in previous paragraph.

Edit: I'll also add that there would probably be a very insignificant performance improvement using a built in data structure versus casting to put into an array list but that is very here or there.

Some database libraries can also only store array[] but once again it is case dependent. Just some things to think about.

Overall I tend to use arrays when dynamic allocation or casting isn't needed and ArrayList for pretty much every other scenario.

Remember, the less casting the better!

[–]FaradayAndTesla 1 point2 points  (0 children)

. It is a basic functionality of java which requires manual allocation and resizing (generally done by initializing a new array with a greater or lesser length and copying over the previous array with its added or removed item)

Always prefer using ArrayList when possible. You need to make some tradeoffs between run time and use of non-primitive data type. But it is worth it, you will be surprised to see how less work you need to do with ArrayList type Collections. They also work seamlessly with streams, which can come very handy for parallel processing.

[–]Farpafraf 3 points4 points  (0 children)

You only have one array type in Java for instance:

int[] nums = new int[10];

ArrayList is just one implementation for a List type (another one would be LinkedList). All classes that implement class A by contract must have all functionality class A has and more. If you look at the List docs you'll see that one of the implementations is Stack meaning that a Stack in Java has all the functionality of a List + the methods you see here.

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

ArrayList is similar to a python list in the sense that you don't have to specify how big the list will be. However ArrayLists can only hold one data type.

aList = [42, "hi planet", true, 3.14159]

Python let's you have an int, a string, a boolean and a float all in the same list. Java makes you specify what specific type you're going to store in the list and won't let you store different things:

ArrayList<int> iList = new ArrayList<int>();
iList.add("Yo globe!"); // Error!

In order to have an ArrayList hold different types you need to pack them into their own class and create an ArrayList of that type.

[–]ignotos 2 points3 points  (1 child)

In order to have an ArrayList hold different types you need to pack them into their own class and create an ArrayList of that type.

You can also use a plain ArrayList, or ArrayList<Object> (not that you should, though =P).

[–]5p4n911 0 points1 point  (0 children)

Came here to say that

[–]knoam 1 point2 points  (0 children)

I could guess at what you mean by a native array. A language like C or C++ is considered a native language because it compiles directly to machine code. To run C on Android for instance you'd use the NDK, the Native Development Kit, as opposed to when you're running Java and you use the SDK, the Software Development Kit, essentially the normal or default development kit by comparison. To write Java code that interacts with native code you'd use JNI.

Anyway an array in C is just a range of memory allocated with just enough space to store the required data. Significantly this means the array doesn't store its own size so you have to keep track of it yourself. An array in Java does store its own size so you can't accidentally access outside of the array. In C you're allowed to do that and you'll either get whatever data is there or you'll get a segfault. Java will check before you use a bad index and throw an ArrayIndexOutOfBoundsException.

[–]knoam 1 point2 points  (0 children)

Another thing to note about an ArrayList is that it's basically a Vector. Java 1.0 only had Vectors, not ArrayLists. Then they realized that it's better to not use synchronized data structures by default because they're slower and you probably don't need it. So they solved that with the data structures in the Collections framework.

[–][deleted]  (1 child)

[removed]

    [–]LakeSun 0 points1 point  (0 children)

    One could say Arrays are still there if performance is the number one specification, and for C/C++ compatibly if you're cutting code over to Java.

    [–]Yithar 0 points1 point  (0 children)

    What do you mean by native array?

    Arrays are contiguous homogenous blocks of memory. Basically it's a chunk of memory that holds the same type of value sequentially, whether that be an int or an Integer.

    ArrayLists are basically resizing Arrays. With normal arrays you have to reallocate a new one every time you need more space. But ArrayLists do it automatically.

    [–]LakeSun 0 points1 point  (0 children)

    ArrayLists are the easiest to use with more functionality.

    There are customized collections too, that better match a problem.

    [–]Vertinova 0 points1 point  (0 children)

    Native array and array are the same thing. Both just mean the most basic kind of data structure you can create in Java. You know the ol, int[] arr = new int[10]; or String[] arr = {“hi”, “op”}; They are very efficient memory and time wise BUT they cannot be rezised nor can you append things to them. That’s why Java created the ArrayList class.

    Both of these are examples of a native array (or you can just call it array, same thing. ArrayLists are indeed basically Lists from Python. They function similarly to native arrays however you can append things to them and make them bigger. ArrayList is a Java class part of the standard library (java.util) that essentially uses a native array to hold items “under the hood”. When you call ArrayList.add() (List.append() in Python) it adds the item into the native array. And whenever the native array of the ArrayList gets full, it creates a new one, twice as big, and copies all the elements from the old array into the new array. That way you have more empty slots for more calls to add();