Notes From Chapter Array in "Thinking in Java"
Array
Array is Java’s most efficient way to store and randomly access a sequence of object references.
The cost of this speed is that the
size
of an array object isfixed
and cannot be changed for the lifetime of that array.ArrayList is measurably less efficient than an array as the cost of size flexibility overhead.
An array can hold primitives, whereas a pre-generic container could not.
- With generics, however, containers can specify and check the type of objects they hold, and with
autoboxing
containers can act as if they are able to hold primitives. - With the advent of autoboxing, the only remaining advantage to arrays is efficiency.
- With generics, however, containers can specify and check the type of objects they hold, and with
arrays use
[ ]
for accessing elements, and a List uses methods such asadd( )
andget( )
. The similarity between arrays and the ArrayList is intentional.array identifier is actually a reference to a true object that’s created on the heap.
Part of the array object is the read-only
length
member that tells you how many elements can be stored in that array object. The[ ]
syntax is the only other access that you have to the array object.You can’t find out how many elements are actually in the array, since
length
tells you only how many elements can be placed in the array; that is, the size of the array object, not the number of elements it actually holds.you should
prefer containers to arrays
when programming in recent versions of Java. Only when it’s proven that performance is an issue (and that switching to an array will make a difference) should you refactor to arrays.
Multidimensional arrays
- primitive array values are automatically initialized if you don’t give them an explicit initialization value. Arrays of objects are initialized to
null
.
public class MultidimensionalPrimitiveArray {
public static void main(String[] args) {
int[][] a = {
{ 1, 2, 3, },
{ 4, 5, 6, },
};
System.out.println(Arrays.deepToString(a));
}
} /* Output:
[[1, 2, 3], [4, 5, 6]]*/
public class ThreeDWithNew {
public static void main(String[] args) {
// 3-D array with fixed length:
int[][][] a = new int[2][2][4];
System.out.println(Arrays.deepToString(a));
}
} /* Output:
[[[0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0]]]*/
Initialize
List<List<someType>> ans = new LinkedList<>();
Queue<someType> queue = new LinkedList<>();
Stack<someType> stack = new Stack<>();
Length
Length() tends to refer to contiguous elements - a string has a length for example.
Size() tends to refer to the size of the collection, often this can be different from the length in cases like vectors (or strings), there may be 10 characters in a string, but storage is reserved for 20.
Count() tends to refer to the number of elements in a looser collection. -from site
arrayInstance.length; //no ()!
stringInstance.length(); //need ()!
listInstance.size();
queueInstance.size();
stackInstance.size();
Add
listInstance.add(someObj);
queueInstance.offer(someObj); //can also use add(someObj)
stackInstance.push(someObj);
Remove
listInstance.remove(someEle);
queueInstance.poll(someEle);
stackInstance.pop();
Access
listInstance.get(someIdx);
queueInstance.peek();
stackInstance.peek();
Difference between the add and offer methods in a Queue in Java?
when element can not be added to collection the add method throws an exception and offer doesn't.