I started working on my Java TicTacToe application this week. When it came to picking a data structure for my board, I reached for
Array. But then I discovered Java also had
What are the differences between
ArrayList in Java? Why use one over the other?
Arrayis a fixed length data structure, ArrayList is variable length. Once created, you can't change an
ArrayListcan be resized dynamically.
ArrayList cannot hold primitives, it can only hold objects. Array can hold both primitives and objects.
A note on boxing and unboxing
Boxing: wrapping a primitive into a class (e.g.
Boolean), after which it becomes a boxed primitive.
Unboxing: converting a wrapped primitive into a primitive.
java> Integer wrappedInt = 1; => java.lang.Integer wrappedInt = 1 java> wrappedInt.intValue(); => java.lang.Integer res1 = 1
Autoboxing: automatically convert from a primitive to an object.
ArrayList<Integer> intList = new ArrayList<Integer>(); intList.add(1);
In this example, autoboxing converted our
1 primitive into an object before adding it to the ArrayList. We didn't actually store a primitive.