Arrays
int[] myArray = new int[10]; // size of array is always after the equals
int[] myArray = new int[10]{ 3, 2, 1 } ;
‘[]’ can be anywhere before the equals:
- int[] myArray
- int [] myArray
- int []myArray
- int myArray[]
- int myArray []
Can be sorted using Arrays class.
Arrays.sort(theArray, optionalComparator)
Public interface Comparator<T>{
int compare(T t1, T t2); // returns 0 if same.
}
Can perform binary search for items but must be sorted before calling.
Arrays.binarySearch(theArray, theKey)
Can compare two arrays. Compares length of two arrays, and if the same length, compares contents.
Arrays.compare(array1, array2)
Collections
Interface | Concrete Class Examples |
List | ArrayList, LinkedList |
Set | HashSet, TreeSet |
Map | HashMap, TreeMap |
Queue | LinkedList |
Deque (Double Ended Queue) | ArrayDeque |
Stack |
Array/List Conversion
Object[] objArray = myArrayList.toArray();
String[] myArray = myArrayList.toArray(new String[0]);
List<String> myList = Arrays.asList(myArray); // set length, but can change entries
List<String> myList = List.of(myArray); // immutable copy
List<String> myList = List.copyOf(myArray); // immutable copy, values copied