Array in Java

  • The array is a fixed-size collection of data. We need to specify the size of the array while creating it, and the size cannot be changed.
  • We have random access to each element in the array by using its index. Indexes start with 0.
  • Array works with references as Objects do.
String[] colors = new String[5];
colors[0] = "red";
colors[1] = "blue";
colors[2] = "white";
colors[3] = "yellow";
colors[4] = "green";
colors[0] = "red";
System.out.println(colors[0]); // red
int[] numbers = new int[3];
numbers[0] = 11;
numbers[1] = 25;
numbers[2] = 39;
String[] names = {"John", "Bek", "Alex", "Donald"};
System.out.println(names[0]); // John
System.out.println(names[3]); // Donald
int[][] numbers = new int[5][5];
numbers[0][0] = 1;
numbers[0][1] = 2;
numbers[0][2] = 3;
// ..
int[] arrNum = new int[3];
System.out.println(arrNum[0]); // 0
System.out.println(arrNum[1]); // 0
System.out.println(arrNum[2]); // 0
String[] colors = new String[3];
System.out.println(colors[0]); // null
System.out.println(colors[1]); // null
System.out.println(colors[2]); // null
int[] arrNum = new int[3];
arrNum[3] = 9; // ArrayIndexOutOfBoundsException
import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int[] myArr = {1, 55, 3, 8, 21, 25, 73, 90};

// [1, 55, 3, 8, 21, 25, 73, 90]
System.out.println(Arrays.toString(myArr));

makeAllSeven(myArr);

// [7, 7, 7, 7, 7, 7, 7, 7]
System.out.println(Arrays.toString(myArr));
}

public static void makeAllSeven(int[] arrNum) {
for (int i = 0; i < arrNum.length; i++) {
arrNum[i] = 7;
}
}
}
  • we have our void method makeAllSeven that accepts int[] arrNum as argument and will assign value to each element as 7.
  • we can see that after using this method the original value of the array got changed. The reason it got changed is that we are passing the reference that points to the actual array in the memory. The argument of the method will simply take a copy of the reference. It means it will also point to the original array. That’s the reason in the main method when using our myArr references, we see the all sevens after the method run.
String[] colors = new String[6];
System.out.println(colors.length); // 6
public void printArray(String[] arr) {
// we can use .length to loop over our array
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public void printArray(String[] arr) {
for (String str : arr) {
System.out.println(arr);
}
}
import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int[] myArr = {1, 55, 3, 8, 21, 25, 73, 90};

// print using Arrays
// [1, 55, 3, 8, 21, 25, 73, 90]
System.out.println(Arrays.toString(myArr));

// sort using Arrays
Arrays.sort(myArr);

// [1, 3, 8, 21, 25, 55, 73, 90]
System.out.println(Arrays.toString(myArr));

int[] myArr2 = {0, 4, 5, 3};
// compare two arrays using Arrays
System.out.println(Arrays.equals(myArr, myArr2));
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store