CMUQ 15-121 Loops, Strings, I/O, and Random
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
- 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}
- You can declare the array and allocate it with the
- If you create an array with
new
then it is automatically fixed with defaults values:- For
int[]
,double[]
, andchar[]
all values are initialized to0
. - For
boolean[]
all values are initialized tofalse
. - For an array of Objects, all values are initialized to
null
. (We’ll talk more aboutnull
later.)
- For
1.2. Differences with Python
Arrays in Java are not like lists in Python. Here are some important differences:
- All elements in an array must be the same type. (For example, you can’t have both integer and floating point numbers in the same array.)
- Arrays are created with a fixed size and don’t grow/shrink.
- You can’t do slicing. (Remember things like
arr[1:5]
in Python? That doesn’t work in Java.) - Negative indices are not allowed. (Remembers doing
arr[-1]
for the last element of a Python list? That doesn’t work in Java.)
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)); } }