CMUQ 15-121 Arrays



1. Java Arrays

Arrays in Java are a way to store multiple items together in a contiguous location in memory. An array has a fixed size (after you create it) that cannot be changed at runtime.

1.1. Creating an Array

  1. There are two main ways an array is created:
    • You can declare the array and allocate it with the new keyword:
      int[] arr = new int[10];
    • You can declare the array “on the fly” and Java will call new for you:
      int[] anotherArr = {1, -5, 42, 9}
  2. If you create an array with new then it is automatically fixed with defaults values:
    • For int[], double[], and char[] all values are initialized to 0.
    • For boolean[] all values are initialized to false.
    • For an array of Objects, all values are initialized to null. (We’ll talk more about null later.)

1.2. Differences with Python

Arrays in Java are not like lists in Python. Here are some important differences:

1.3. Sample Code

Consider the following sample demonstrating basic array usage. You should copy this into Eclipse and run it, then make changes to it and experiment with it.

public class ArrayDemo {

    public static void main(String[] args) {
        
        // Declare an array and initialize it inline
        int[] arr = {1,5,10,60,32,3,2,-78};
        
        // Print out the number of elements in arr
        System.out.println("There are " + arr.length + " items in arr.");
        
        //Print out the contents of arr
        System.out.println("The [2]'d item in arr is " + arr[2]);
        System.out.print("All the items in arr are: ");
        for(int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println("");
        
        // Declare an array and initialize it using new
        int[] arr2 = new int[5];
        
        // Print out the number of elements in arr2
        System.out.println("\nThere are " + arr2.length + " items in arr2.");
        
        //Print out the contents of arr2
        System.out.print("All the items in arr2 are: ");
        for(int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + " ");
        }
        System.out.println("");        
        
        // Modify some elements of arr2
        arr2[0] = 105;
        arr2[3] = 56;
        
        //Print out the contents of arr2 again 
        System.out.print("All the items in arr2 are: ");
        for(int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + " ");
        }
        System.out.println("");        
    }
    
}

2. The Arrays Class

Java provides some useful methods you can use to manipulate Arrays in the class java.util.Arrays. Click that link now to go to the API and take a look at it. Here is some code that uses a few of its more useful methods:

import java.util.Arrays;

public class ArraysClassDemo {

    public static void main(String[] args) {
        // Declare an array and initialize it inline
        int[] arr = {1,60,32,10,-78};
        
        // Declare another array and initialize it manually
        int[] arr2 = new int[5];
        
        // Print out the number of elements in arr
        System.out.println("There are " + arr.length + " items in arr.");
        
        // Try to print out the contents of an Array using a normal print
        System.out.println("Printing arr directly: " + arr);
        
        //Print out the contents of arr using the Arrays class
        System.out.println("Printing arr using Arrays.toString(): " + Arrays.toString(arr));
        
        // Compare the two arrays:
        if (Arrays.equals(arr,arr2)) {
            System.out.println("The arrays are equal.");
        }
        else {
            System.out.println("The arrays are not equal.");
        }
        
        // Sort arr
        Arrays.sort(arr);
        
        //Print out the contents of arr using the Arrays class
        System.out.println("Sorted arr: " + Arrays.toString(arr));
    }

}