Title: Arrays
1Arrays
2Arrays
- A contiguous collection of cells
- Cells are also known as elements, items,
entries, slots - Each cell contains the same type of item
- The number of cells in an array will never
change! - Cells are given names (in a sense)
- Indices (subscripts) beginning at 0
3Arrays
- When using arrays
- First create the array itself. Must specify
size! - Second place data in each slot
- Third relax
- Syntax for declaring an array variable
- ClassName variableName
- Syntax for creating an array
- new ClassName ArraySize
http//www.stephenadams.freeserve.co.uk/2000/graph
ics/policeman.jpg
4Arrays
- Syntax for accessing an element of the array
- arrayVariable index
- The index must be between 0 and the length of the
array 1. - Syntax for replacing an element of the array
- arrayVariable index someExpression
- The index must be between 0 and the length of the
array -1 - Type conformance rules are still followed
- Syntax for finding the length of an array
- arrayVariable.length
5Array Example
int numbers int number numbers new
int3 numbers0 1 numbers1
-1 numbers2 numbers0numbers1 numbersnu
mbers2 numbers1 number numbers1
6Array Examples
Rectangle rects new Rectangle4 rects0
new Rectangle(10, 20, 30, 40) rects1 new
Rectangle(0, 0, 100, 100) rects2
rects1 Rectangle oneRect new Rectangle(10,
10, 10, 10) rects3 oneRect oneRect
rects0
char bunchOfChars new char20 bunchOfChars0
A bunchOfCharsbunchOfChars.length-1
?
7Arrays
- Can be passed to functions and returned from
functions - Problem write a method that takes an array of
Rectangle objects and sets all of their colors to
red
private void colorAllRed( Rectangle rects )
for(int i0 iltrects.length i)
rectsi.setColor( Color.red )
Rectangle someRects someRects new
Rectangle3 someRects0 new
Rectangle(0,0,5,5) someRects1 new
Rectangle(6,6,8,8) someRects2 new
Rectangle(10,10,10,10) colorAllRed( someRects )
8Initializing arrays
- Arrays can be filled with data when declared
int arr 10, 100, 1000 Oval circles
new Oval(1,1,1,1), new Oval(2,2,2,2)
int arr new int3 arr0 10 arr1
100 arr2 1000 Oval circles new
Oval2 circle0 new Oval(1,1,1,1) circle1
new Oval(2,2,2,2)
9Arrays
- Arrays are Objects (mostly)
- Arrays are not Objects in the normal sense
- Cannot be subclassed
- Hence cannot override methods like toString and
equals
public class MyArraySubclass extends int
public boolean equals(Object other)
10Examples
- Write a program to sum the elements in an array
of doubles.
public double sum(double values) double
result 0 for(int i0 iltvalues.length i)
result valuesi return result
11Examples
- Write a function that accepts an array of doubles
named A and a double named V. The function
returns the index of the first occurrence of V in
A or -1 if X is not in A.
public int indexOf(double a, double v)
for(int i0 ilta.length i) if(v
ai) return i return -1
12Examples
- Write a function that accepts an array of doubles
named A and a double named V. The function
returns an array of all elements of A that are
greater than V.
public double greaterThan(double a, double v)
int count0 for(int i0 ilta.length i)
if(ai gt v) count double result
new doublecount int resultIndex 0 for(int
i0 ilta.length i) if(ai gt v)
resultresultIndex ai return result
13Examples
- Write a function that accepts an array of ints
and shuffles them into a random order. - Do the following 2n times, where n is the number
of elements in the array - select to random indices into the array and swap
the elements in the array
public void shuffle(int values) for(int i0
iltvalues.length2 i) int ranIndex1
(int)(Math.random()values.length) int
ranIndex2 (int)(Math.random()values.length) i
nt tmp valuesranIndex1 valuesrandIndex1
valuesranIndex2 valuesrandIndex2 tmp
int data 1,2,3,4,5 shuffle(data)
14Examples
- Write a function that accepts an array of ints
and returns the smallest index of the smallest
value
/ _at_pre values.length gt 1 / public int
findMin(int values) int minIndex
0 for(int i1 iltvalues.length i)
if(valuesi lt valuesminIndex) minIndex
i return minIndex
15Tic Tac Toe
- Write a TicTacToeModel using arrays!