To try to explain you the difference between an array and an arraylist in java.
An array ist initialized with a defined amount of items it can hold (maximum).
Code:
int[] intArray = new int[5];
would be an array which could hold ints, but maximum 5 of them (position 0 - 4 in array).
An Arraylist is a java class of the standard jdk from oracle. Its oracles implementation of something you can use like you would use an array in another programming language. But the ArrayList is sometimes slower then an actual array (meassuring that time is part of MicroPerfomance-Tests).
So sometimes for perfomance reasons you want to use an "real" array.
You can use an array in java for example when you know the size of the array will never be bigger then a specified amount.
When you're a beginner stick to the ArrayList from java. It will do the things you want in the most easy way.
And maybe take a look at Google Guava later. It reimplements some of the Java Librarys in a so much better way. You can easily define arrays and use them (a must have when you write testclass for example or if you want to return a list from a stream of objects in a function).
Edit:
Array
- you have to know the maximum size of objects at construction
- Its more like you create many variables you can set imagine:
you want to save exaktly three Integers in your class.
Therefore you created this:
Code:
private int firstInt;
private int secondInt;
private int thirdInt;
But you could rather also just use:
Code:
private int[] intArray = new int[3];
and you would assign the different arrays.
Its more like you have 3 variables in one.
you would assign them like:
Code:
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
The ArrayList is a more complex implementation which manages an array for you.
Its an Instance of a class you create and use (see post above). Oracle implemented it for you.
Thats why you can call methods like add() or get() on that object.