char[] chars = {'b', 'a', 'c'};
Arrays.sort(chars);
// [a, b, c]
Arrays.toString(chars);
Java Arrays
Sort
Multidimensional Arrays
int[][] matrix = { {1, 2, 3}, {4, 5} };
int x = matrix[1][0]; // 4
// [[1, 2, 3], [4, 5]]
Arrays.deepToString(matrix)
for (int i = 0; i < a.length; ++i) {
for(int j = 0; j < a[i].length; ++j) {
System.out.println(a[i][j]);
}
}
// Outputs: 1 2 3 4 5 6 7
Loop (Read)
String[] arr = {"a", "b", "c"};
for (int a: arr) {
System.out.print(a + " ");
}
// Outputs: a b c
Loop (Read & Modify)
int[] arr = {1, 2, 3};
for (int i=0; i < arr.length; i++) {
arr[i] = arr[i] * 2;
System.out.print(arr[i] + " ");
}
// Outputs: 2 4 6
Modify
int[] a = {1, 2, 3};
System.out.println(a[0]); // 1
a[0] = 9;
System.out.println(a[0]); // 9
System.out.println(a.length); // 3
Declare
int[] a1;
int[] a2 = {1, 2, 3};
int[] a3 = new int[]{1, 2, 3};
int[] a4 = new int[3];
a4[0] = 1;
a4[2] = 2;
a4[3] = 3;
Comments