Basics

int [] intArray = new int[5]
 
int[] intArray;
intArray = new int[5]; // 크기 5의 빈 배열
 
int[] intArray = {1, 2, 3, 4, 5};

Aliasing

int[] arr1 = {1,2,3,4,5};
int[] arr2 = arr1;
  • arr1 and arr2 point to the same address, so arr2 is an alias to arr1

Cloning

int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = arr1.clone();
 
arr1[0] = 100;
System.out.println(arr1[0]); // 100
System.out.println(arr2[0]); // 1

For-each

for (int i : intArray)
{
	System.out.println(i);
}
  • X getting the index but the element itself

Multi array

int[][] multiArray = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};
int[][] multiArray = new int[3][4];

Different ways of creating arrays

List.of

  • Makes it immutable
List<String> list = List.of("A", "B", "C");

Arrays.asList(...)

  • Fixed-size List backed by an array
List<String> list = Arrays.asList("A", "B", "C");

new ArrayList<>()

  • Mutable List
List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));