Arrays
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();
Visualizing Arrays
0 | 1 | 2 | 3 | 4 |
| | | | |
Cat[] cats = new cat[5];
When you create an array, it starts with [n] empty elements
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
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
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();
Declaring an Array Variable
Type[] var;
[] is pronounced “array”
Instantiating an Array Object
new Type[100];
How many elements are in this array
Assigning a value to an Array
Type[] var = new Type[100];
var[6] = value;
How many elements
Which element is being assigned a value
Getting a value to an Array
Type[] var = new Type[100];
Type elem = var[42];
Which element is being getted
How many elements
Getting the length of an Array
Type[] var = new Type[100];
int len = var.length;
ArrayList uses .size()
Arrays use .length
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];
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
Funsheet
Iterating over an array
Type[] arr = new Type[100];
for(Type elem : arr)
{
System.out.println( elem );
}
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 );
}
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
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 |
Activity
public boolean contains(int target, int[] arr)
{
Complete this method
(use a foreach loop)
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!
Activity
public int search(int target, int[] arr)
{
Complete this method
(use a regular for loop)
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;�}
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
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;
}
Animation and Frames
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”);
…
Timing
int timer = 0;
int currentFrame = 0;
MayflowerImage[] frames = new MayflowerImage[8];
Act Method Algorithm:
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!
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
}
}
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?
public void act()
{
timer++;
if(timer >= 30)
{
timer = 0;
currentFrame++;
if(currentFrame >= frames.length)
{
currentFrame = 0;� }
setImage(frames[currentFrame]);
}
}
What is 7 % 7 ?
public void act()
{
timer++;
if(timer >= 30)
{
timer = 0;
currentFrame++;
if(currentFrame >= frames.length)
{
currentFrame = 0;� }
setImage(frames[currentFrame]);
}
}
What is 0 % 7 ?
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 ?
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
Lab 4.1