Java Code Example: Edit/compare multiple arrays

Arrays provide some very useful static methods that we can apply directly to our array. For example, for sorting, searching, comparing, etc.

import java.util.Arrays;

public class ArrayMethodsExample {
	public static void main(String[] args) {
		int[] myFirstArray = { 1, 2, 3, 4 };
		int[] mySecondArray = { 5, 6, 7, 8 };
		int[] myThirdArray = { 5, 6, 7, 8 };
		String[] myFourthArray = { "a", "b", "c", "d" };


		boolean isSame = Arrays.equals(mySecondArray, myThirdArray);
		boolean inArray = Arrays.asList(myFourthArray).contains("a");

		System.out.print("Fill Array: ");
		Arrays.fill(myFirstArray, 0, 2, 5);
		for (int i = 0; i < myFirstArray.length; i++) {
			System.out.print(myFirstArray[i]);
		}

		System.out.print("\nSorted Array: ");
		Arrays.sort(myFirstArray);
		for (int i = 0; i < myFirstArray.length; i++) {
			System.out.print(myFirstArray[i]);
		}

		System.out.println("\ncontains: " + inArray);
		System.out.println("array as string: " + Arrays.toString(myThirdArray));
		System.out.println("length before copyOfRange: " + myThirdArray.length);
		myThirdArray = Arrays.copyOfRange(myThirdArray, 0, 10);
		System.out.println("length after copyOfRange: " + myThirdArray.length);
		System.out.println("equals: " + isSame);

	}
}
Output
Fill Array: 5534
Sorted Array: 3455
contains: true
array as string: [5, 6, 7, 8]
length before copyOfRange: 4
length after copyOfRange: 10
equals: true