Array key point:
- Not scalable, once constructed length cannot be change,
- When construct numeric primitive type, it will filled with default value. byte, short, int, long, double, float 0
- char filled with empty.
- boolean filled with false.
- object filled with null.
public static void main(String[] args) {
byte[] bytesArray = new byte[1];
System.out.println(bytesArray[0]);
short[] shortArray = new short[1];
System.out.println(shortArray[0]);
char[] charArray = new char[1];
System.out.println(charArray[0]);
int[] intArray = new int[1];
System.out.println(intArray[0]);
long[] longArray = new long[1];
System.out.println(longArray[0]);
float[] floatArray = new float[1];
System.out.println(floatArray[0]);
double[] doubleArray = new double[1];
System.out.println(doubleArray[0]);
}
## Output ##
0
0
0
0
0.0
0.0
Copy by reference
When copy array using equal assignment it will point to the origin reference. Below example shown both array1 and array2 elements are same.
int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = array1;
array2[4] = 7;
System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(array2));
## Output ##
[1, 2, 3, 4, 7]
[1, 2, 3, 4, 7]
Copy by value
To copy one from another without interfere it’s original value, use Arrays.copyOf.
int[] array1 = { 1, 2, 3, 4, 5 };
int[] newarray = Arrays.copyOf(array1, array1.length);
newarray[4] = 7;
System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(newarray));
## Output ##
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 7]
Arrays.fill
Arrays class provides methods that let you fill/replace all elements with additionally supply with start and end index.
String[] transport = {"car", "bike", "van", "lori"};
Arrays.fill(transport, 1, 2, "bus");
System.out.println(Arrays.toString(transport));
## Output ##
[car, bus, van, lori]
Arrays.equals
To compare 2 array elements equality, we can use Arrays.equals.
int a[] = { 1, 2, 3, 4};
int b[] = { 2 ,4 ,6 ,8};
int c[] = { 1, 2, 3 ,4};
System.out.println(Arrays.equals(a, b)); // false
System.out.println(Arrays.equals(a, c)); // true
System.out.println(Arrays.equals(b, c)); // false
ArrayList
ArrayList key point:
- Only support primitive wrapper and object.
- Scalable, create new array internally when insufficient size.
- ArrayList is a generic class
ArrayList<Integer> numbers = new ArrayList<>();
Copy by Reference
ArrayList work as same when assigned to new variable. One changed will affected anther to their origin values.
ArrayList<Integer> digits = new ArrayList<>();
digits.add(1);
digits.add(2);
digits.add(3);
ArrayList<Integer> newDigit = digits;
newDigit.set(0, 10);
newDigit.set(1, 20);
newDigit.set(2, 30);
System.out.println(digits.toString());
System.out.println(newDigit.toString());
## Output ##
[10, 20, 30]
[10, 20, 30]
Copy by value
To copy by it’s value, use constructor parameters.
ArrayList<Integer> digits = new ArrayList<>();
digits.add(1);
digits.add(2);
digits.add(3);
ArrayList<Integer> newDigit = new ArrayList<>(digits);
newDigit.set(0, 10);
newDigit.set(1, 20);
newDigit.set(2, 30);
System.out.println(digits.toString());
System.out.println(newDigit.toString());
## Output ##
[1, 2, 3]
[10, 20, 30]
Collection.fill
Similar to Arrays.fill, ArrayList can be fill by using Collection.fill but it cannot specify with start and end index.
String[] transport = {"car", "bike", "van", "lori"};
List<String> list = Arrays.asList(transport);
Collections.fill(list, "unknown");
System.out.println(list);
Array and ArrayList as Parameter
When Array or ArrayList pass as parameter, we refer to pass as reference. Which mean the method received a reference to the the array.
public static void main(String[] args) {
int[] param = {1, 2, 3, 4, 5, 6};
System.out.println("Orignal " + Arrays.toString(param));
refByValue(param);
System.out.println("Param" + Arrays.toString(param));
param = test1(param);
System.out.println("Test 1: " + Arrays.toString(param));
test2(param);
System.out.println("Test 2j: " + Arrays.toString(param));
}
public static int[] test1(int[] param) {
int[] newParam = param;
Arrays.fill(newParam, 0);
return newParam;
}
public static void test2(int[] param) {
Arrays.fill(param, 100);
}
public static void refByValue(int[] param) {
int[] arr = Arrays.copyOf(param, param.length);
Arrays.fill(arr, 0);
System.out.println("newParam: " + Arrays.toString(arr));
}
## Output ##
Orignal [1, 2, 3, 4, 5, 6]
newParam: [0, 0, 0, 0, 0, 0]
Param[1, 2, 3, 4, 5, 6]
Test 1: [0, 0, 0, 0, 0, 0]
Test 2j: [100, 100, 100, 100, 100, 100]