Transcript Arrays

Arrays
In this section we will learn how about storing groups of related variables that
share the same type in arrays:
 Declaring arrays
 Arrays and Memory
 Accessing Arrays
 Multidimensional Arrays
 Arrays of Objects
PHY281 Scientific
Java Programming
Arrays
Slide 1
Declaring Arrays
One of the most powerful aspects of computers is their ability to perform the
same calculation thousands of times on different data. To program this you
need a loop and the ability to store multiple variables of the same type.
i.e. rather than have several integers a, b, c, d ... have an array of integers
with a single name.
You declare an array just like a normal variable but uses square brackets [ ] to
indicate it is an array:
Declares primes to be an
array of integers
int[] primes;
int[] primes = new int[10];
Declares primes to be an array
of integers and defines it to
have 10 elements
int[] primes = {2, 3, 5, 7, 11, 13, 17};
PHY281 Scientific
Java Programming
Arrays
Declares primes to be an array
of integers and defines it to
have 7 elements with the
values given
Slide 2
Arrays and Memory
int[] primes = {2, 3, 5, 7, 11, 13, 17};
This allocates 7 consecutive memory locations and fills them with the values given.
primes[1]
primes[3]
primes[5]
primes[0]
primes[2]
primes[4]
primes[6]
2
memory:
3
5
7
11
13
index
17
The index runs from [0] to [size-1]
In this example if you try and access primes[7] you will get garbage or crash the
program:
int a = primes[0];
int b = primes[6];
int c = primes[7];
PHY281 Scientific
Java Programming
sets a to 2
sets b to 17
gives an error
Arrays
Slide 3
Accessing Arrays
To access an array you can treat the individual elements just like normal variables
sum12 = primes[1] + primes[2];
arrayVar[23]++;
g.drawString("Name = " + surname[index], 50, 50);
You can find out the size of the array using a variable called length
int[] primes = { 2, 3, 5, 7, 11, 13, 17};
int len = primes.length;
g.drawString("Length = " + len , 50 ,40);
g.drawString("The first is " + primes[0], 50, 60);
g.drawString("The last is " + primes[len-1], 50, 80);
Length = 7
The first is 2
The last is 17
PHY281 Scientific
Java Programming
Arrays
Slide 4
Array Example
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class MarkArray extends Applet {
}
public void paint(Graphics g) {
int[] marks = {45, 55, 56, 76, 36, 78, 67, 86};
int y = 20;
double sum = 0.0;
for (int i = 0; i < marks.length; i++) {
sum = sum + marks[i];
g.drawString("Mark " + (i+1) + " is " + marks[i], 50, y);
y = y + 20;
}
double ave = sum / marks.length;
g.drawString("The average is " + ave, 50, y);
}
PHY281 Scientific
Java Programming
Arrays
Slide 5
Multidimensional Arrays
To create arrays with more dimensions use extra brackets [ ] e.g.
double[] xPoints = new double[10];
double[] xyPoints = new double[10][10];
double[] xyzPoints = new double[10][20][30];
Creates a 3 dimensional array
with 6,000 elements
You access them just like one dimensional ones:
xValue = xPoints[7];
xyPoints[5][3] = 2.45;
xyzValue = xyzPoints[xCoord][yCoord][zCoord];
PHY281 Scientific
Java Programming
Arrays
Slide 6
Arrays of Objects
Arrays can hold anything not just numbers, but strings and objects
such as buttons etc.
(A String is a collection of characters, note: the capital S for
string (it's an Object not a simple variable))
String[] beatles = {"John", "Paul", "George", "Ringo" };
char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
This code will create an array of buttons labelled 0 to 9.
Button[] digit = new Button[10];
Converts the number b to a string
for (int b = 0; b < 10; b++) {
String label = Integer.toString(b);
digit[b] = new Button(label);
add(digit[b]);
digit[b].addActionListener(this);
}
PHY281 Scientific
Java Programming
Arrays
Normal code for
adding a button
Slide 7
An Array of Buttons
public class ButtonArray extends Applet
implements ActionListener{
}
int buttonNumber = -1;
Button[] digit = new Button[10];
public void init() {
for (int b = 0; b < 10; b++) {
String label = Integer.toString(b);
digit[b] = new Button(label);
add(digit[b]);
digit[b].addActionListener(this);
}
}
public void paint(Graphics g) {
if (buttonNumber > -1) {
g.drawString("You pressed " + buttonNumber, 50, 100);
}
}
public void actionPerformed(ActionEvent e) {
buttonNumber = Integer.parseInt(e.getActionCommand());
repaint();
}
Gets the label of the button and converts it to a number
PHY281 Scientific
Java Programming
Arrays
Slide 8