1 of 26

Arrays

2 of 26

Collections

  • We would like to be able to keep lots of information at once
  • Example: Keep all the students in the class
  • Grade each one without writing each one out
  • Collections are data types that hold groups of data

3 of 26

Arrays

  • An array is a data type that keeps data in a line.
  • All the elements in the array have to be the same type.
  • Arrays aren’t primitives, but their syntax isn’t really like objects

4 7 3 12 -4 87 0 45

4 of 26

Arrays v. ArrayLists

Array

ArrayList

Fixed Size

Grows and Shrinks

All objects in collection the same type

All objects in collection the same type

Weird syntax

Standard syntax

No methods

Lots of methods

Can quickly initialize

Must add objects one at a time

Can use primitives or objects

Objects only - use wrapper classes for primitives

5 of 26

Array types

Declare an array:

Car[] parkingLot;

int[] numbers;

String[] words;

6 of 26

Create arrays: Way 1

int[] numbers = new int[5];

Makes 5 empty “slots”, indexes are 0-4

String[] words = new String[10];

Car[] parkingLot = new Car[100];

7 of 26

Create arrays: Way 2

int[] numbers = {4,6,8,10,12};

String[] words = {“happy”, “go”, “lucky”};

8 of 26

Array Indices

  • Elements in an array are numbered for convenience
  • Numbering starts at 0, like Strings

4 7 3 12 -4 87 0 45

0 1 2 3 4 5 6 7

9 of 26

Array Indices

  • Can access or change elements in the array using their indices:

int x = nums[3];

nums[0] = -1*nums[0];

4 7 3 12 -4 87 0 45

0 1 2 3 4 5 6 7

10 of 26

Array Length

Access the length of any array with .length

int len = myNumbers.length;

No parentheses!!! Arrays don’t have methods.

11 of 26

Examples

  • Make an array of 100 integers, where each number in the array is equal to twice its index value
  • Sum up all numbers in an array
  • Write a method that reverses an array.
  • Find the middle number in an array of ints
  • Find the biggest number in an array of ints

12 of 26

Empty Arrays

What will print?

int[] nums = new int[10];

System.out.println(nums[5]);

What value is in the “empty” array?

Note - the length is 10, even though I haven’t set any of the values yet.

13 of 26

Initializing Arrays

Java zeroes out the memory in an array, so the value of the boxes in the empty array is whatever “0” means for your type:

int, double -> 0

boolean -> false

char -> some weird character

Objects -> NULL

14 of 26

Null Objects

  • Null objects have not been created yet. They are placeholders only
  • If you try to call a method on a null object, it crashes
  • NullPointerException

15 of 26

Null Objects

String[] words = new String[3];

int length = words[0].length();

// CRASH!!

After making an array, make sure you initialize the objects inside the array

16 of 26

Visualizing an Array of Objects

  • Create the array

Car[ ] myCars = new Car[5];

  • Create the objects�for(int i = 0; i< myCars.length; i++){� myCars[i] = new Car();

}

0 1 2 3 4

Ø Ø Ø Ø Ø

myCars

17 of 26

Tricky Stuff to Remember

  • Arrays can be input or output to methods just like any other data type, but methods might change the array.�
  • Arrays must be copied explicitly

int[ ] nums1 = {1,2,3};�int[ ] nums2 = nums1;

This doesn’t do it. It just gives 2 names for the same array

18 of 26

Tricky Stuff to Remember

  • Don’t print directly (System.out.println won’t do it). Use Arrays.toString().
  • Or print elements individually in a loop

19 of 26

More Exercises

  • Create an array of 50 BankAccounts. Then deposit $100 in each.

  • Take an array of Car objects and return the Car with the lowest gas mileage. (Assume there is a getGasMileage() method).� public Car lowestGasMileage(Car[] myCars)

  • Take an array of Students and return the student object whose name is given. Assume there is a getName() method.

public Student hasName(Student[] students, String name)

20 of 26

Multidimensional Arrays

  • We can also create arrays of arrays (matrices)
  • int[ ][ ] matrix = new int[2][4];

0 1 2 3

� 0�� 1

21 of 26

Multidimensional Arrays

We visualize this

But it really is more like this

0 1 2 3

� 0�� 1

0 1

0 1 2 3

0 1 2 3

22 of 26

Multidimensional Arrays

arr.length gives the number of rows

arr[0].length gives the number of columns

arr[r][c] gives element at row r, column c

arr[r] gives the entire rth row

0 1

0 1 2 3

0 1 2 3

23 of 26

Initializing Shortcut

  • int[ ][ ] matrix = { {0,3,2,4},{8,9,2,1} };

0 1 2 3

� 0�� 1

0 3 2 4�� 8 9 2 1

24 of 26

Multidimensional Arrays

  • myNumbers[1][2] = 1
  • myNumbers.length is the number of rows
  • myNumbers[0] is the first row
  • myNumbers[0].length is the number of elements in the first row (same as number of columns)

0 1 2 3

� 0�� 1

2 3 -1 4�� 7 4 1 8

myNumbers

25 of 26

Array Patterns

  • Access every element in an array:

for(int i = 0; i<array.length; i++){� int x = array[i];�}

  • Access every element in a matrix:

for(int i = 0; i<array.length; i++){� for(int j = 0; j< array[i].length; j++){� int x = array[i][j];� }�}

26 of 26

Exercises

  • Create a multiplication table � (mult[a][b] = axb)

public int[][] multTable(int a, int b){

  • Add all the elements in a table

public int addUp(int[][]myTable){

  • Print the indices of a number in the table

public void findIndices(int target, int[][] table){