1 of 35

Arrays

2 of 35

Arrays are like Lists

ArrayList<Cat> cats = new ArrayList<Cat>();

cats.add( new Cat());

cats.add( new Cat());

cats.add( new Cat());

cats.add( new Cat());

cats.add( new Cat());

Cat[] cats = new cat[5];

cats[0] = new Cat();

cats[1] = new Cat();

cats[2] = new Cat();

cats[3] = new Cat();

cats[4] = new Cat();

3 of 35

Visualizing Arrays

0

1

2

3

4

Cat[] cats = new cat[5];

When you create an array, it starts with [n] empty elements

4 of 35

Visualizing Arrays

0

1

2

3

4

Cat[] cats = new cat[5];

cats[0] = new Cat();

You can use square bracket syntax to assign values to elements

5 of 35

Visualizing Arrays

0

1

2

3

4

Cat[] cats = new cat[5];

cats[0] = new Cat();

cats[3] = new Cat();

You can assign values to

any index in the array

6 of 35

Visualizing Arrays

0

1

2

3

4

Cat[] cats = new cat[5];

cats[0] = new Cat();

cats[1] = new Cat();

cats[2] = new Cat();

cats[3] = new Cat();

cats[4] = new Cat();

7 of 35

Declaring an Array Variable

Type[] var;

[] is pronounced “array”

8 of 35

Instantiating an Array Object

new Type[100];

How many elements are in this array

9 of 35

Assigning a value to an Array

Type[] var = new Type[100];

var[6] = value;

How many elements

Which element is being assigned a value

10 of 35

Getting a value to an Array

Type[] var = new Type[100];

Type elem = var[42];

Which element is being getted

How many elements

11 of 35

Getting the length of an Array

Type[] var = new Type[100];

int len = var.length;

ArrayList uses .size()

Arrays use .length

12 of 35

Primitive Arrays

You can create arrays of primitive data types

int[] nums = new int[10];

double[] decimals = new double[42];

boolean[] flags = new boolean[8];

13 of 35

Array vs ArrayList

Array vs ArrayList

ArrayLists begin as empty lists and you add elements to them.

Arrays begin with a specified length and you assign values to those elements

ArrayLists use methods like add(), remove(), and set() to manipulate their data

Arrays use square bracket syntax and can only get and set their values (no remove, add, or insert)

ArrayLists can only contain Object data (wrapper classes are used to store primitive data)

Arrays can store primitive data without wrapper classes.

ArrayList

new ArrayList<Type>()

begins empty

Uses add(), remove()

to grow/shrink size

Uses set(), get()

to change/get values

Only objects can be made into Lists

(primitive types must use Wrapper Classes)

Array

new Type[n]

begins with n elements

Length cannot change

Uses square bracket syntax

to change/get values

Primitive types can be made into arrays

14 of 35

Funsheet

15 of 35

Iterating over an array

Type[] arr = new Type[100];

for(Type elem : arr)

{

System.out.println( elem );

}

16 of 35

Iterating over an array

Type[] arr = new Type[100];

for(int i=0; i<arr.length; i++)

{

Type elem = arr[i];

System.out.println( elem );

}

17 of 35

Activity

Consider the array:

int[] nums = new int[5];

nums[0] = 5;

nums[1] = 10;

nums[2] = 15;

nums[3] = 20;

nums[4] = 25;

Draw a picture of this array

18 of 35

Activity

Consider the array:

int[] nums = new int[5];

nums[0] = 5;

nums[1] = 10;

nums[2] = 15;

nums[3] = 20;

nums[4] = 25;

0

1

2

3

4

5

10

15

20

25

19 of 35

Activity

public boolean contains(int target, int[] arr)

{

Complete this method

(use a foreach loop)

20 of 35

Activity

public boolean contains(int target, int[] arr)

{

for(int val : arr)

{

if(val == target)

{

return true;

}� }

return false;

}

This code is exactly the same whether arr is an array or an ArrayList!

21 of 35

Activity

public int search(int target, int[] arr)

{

Complete this method

(use a regular for loop)

22 of 35

Activity

public int search(int target, int[] arr)

{

for(int i=0; i<arr.length; i++)

{

int val = arr[i];

if(val == target)

{

return i;

}

}

return -1;�}

23 of 35

Activity

public int getLargest(int[] arr)

{

Complete this method

(return the largest value in arr)

Algorithm

Assume the first element is the largest.

If any element in the array is larger than what you think is the largest, set that value as the largest

24 of 35

Activity

public int getLargest(int[] arr)

{

int largest = arr[0]; // assume the first value is the largest

for(int val : arr)

{

if(val > largest) // if val is larger than largest…

{

largest = val; // then set val as largest

}� }

return largest;

}

25 of 35

Animation and Frames

26 of 35

Arrays and Animations

0

1

2

3

4

5

6

7

MayflowerImage[] frames = new MayflowerImage[8];

frames[0] = new MayflowerImage(“walking 1.png”);

frames[1] = new MayflowerImage(“walking 2.png”);

27 of 35

Timing

int timer = 0;

int currentFrame = 0;

MayflowerImage[] frames = new MayflowerImage[8];

Act Method Algorithm:

    • Increment timer
    • If timer has reached 30
      • Reset timer to 0
      • Increment current frame index
      • Get image from array at current frame index
      • Set image to that image

28 of 35

Funtivity: Write code change the image every 30 ticks.

public class Animatron extends Actor

{

private MayflowerImage[] frames;

private int currentFrame;

private int timer;

public void act()

{

Assume the constructor instantiates these variables

Hint: Don’t go out of bounds of the array!

29 of 35

Funtivity: Write code change the image every 30 ticks.

public void act()

{

timer++; // increment timer

if(timer >= 30)

{

timer = 0; // reset timer

currentFrame++; // increment currentFrame

if(currentFrame >= frames.length) // make sure currentFrame

{ // doesn’t go out of bounds!

currentFrame = 0;� }

setImage(frames[currentFrame]); // change image to new frame

}

}

30 of 35

public void act()

{

timer++;

if(timer >= 30)

{

timer = 0;

currentFrame++;

if(currentFrame >= frames.length)

{

currentFrame = 0;� }

setImage(frames[currentFrame]);

}

}

If there are 7 elements in frames, what will currentFrame be when this if statement evaluate to true?

31 of 35

public void act()

{

timer++;

if(timer >= 30)

{

timer = 0;

currentFrame++;

if(currentFrame >= frames.length)

{

currentFrame = 0;� }

setImage(frames[currentFrame]);

}

}

What is 7 % 7 ?

32 of 35

public void act()

{

timer++;

if(timer >= 30)

{

timer = 0;

currentFrame++;

if(currentFrame >= frames.length)

{

currentFrame = 0;� }

setImage(frames[currentFrame]);

}

}

What is 0 % 7 ?

33 of 35

public void act()

{

timer++;

if(timer >= 30)

{

timer = 0;

currentFrame++;

if(currentFrame >= frames.length)

{

currentFrame = 0;� }

setImage(frames[currentFrame]);

}

}

What is 1 % 7 ?

What is 2 % 7 ?

What is 3 % 7 ?

What is 4 % 7 ?

What is 5 % 7 ?

What is 6 % 7 ?

34 of 35

public void act()

{

timer++;

if(timer >= 30)

{

timer = 0;

currentFrame++;

currentFrame = currentFrame % frames.length;

setImage(frames[currentFrame]);

}

}

0 % n = 0

1 % n = 1

2 % n = 2

3 % n = 3

n-1 % n = n-1

n % n = 0

35 of 35

Lab 4.1