Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHY281 Scientific Java Programming ArraysSlide 1 Arrays In this section we will learn how about storing groups of related variables that share the same.

Similar presentations


Presentation on theme: "PHY281 Scientific Java Programming ArraysSlide 1 Arrays In this section we will learn how about storing groups of related variables that share the same."— Presentation transcript:

1 PHY281 Scientific Java Programming ArraysSlide 1 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

2 PHY281 Scientific Java Programming ArraysSlide 2 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: int[] primes = new int[10]; int[] primes; Declares primes to be an array of integers Declares primes to be an array of integers and defines it to have 10 elements int[] primes = {2, 3, 5, 7, 11, 13, 17}; Declares primes to be an array of integers and defines it to have 7 elements with the values given

3 PHY281 Scientific Java Programming ArraysSlide 3 Arrays and Memory int[] primes = {2, 3, 5, 7, 11, 13, 17}; 2 3 5 7 11 13 17 This allocates 7 consecutive memory locations and fills them with the values given. primes[0] primes[1] primes[6]primes[2] primes[3] primes[4] primes[5] index memory: 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]; sets a to 2 sets b to 17 gives an error

4 PHY281 Scientific Java Programming ArraysSlide 4 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

5 PHY281 Scientific Java Programming ArraysSlide 5 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); }

6 PHY281 Scientific Java Programming ArraysSlide 6 Multidimensional Arrays double[] xPoints = new double[10]; double[] xyPoints = new double[10][10]; double[] xyzPoints = new double[10][20][30]; To create arrays with more dimensions use extra brackets [ ] e.g. 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];

7 PHY281 Scientific Java Programming ArraysSlide 7 Arrays of Objects Arrays can hold anything not just numbers, but strings and objects such as buttons etc. String[] beatles = {"John", "Paul", "George", "Ringo" }; char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' }; Button[] digit = new Button[10]; for (int b = 0; b < 10; b++) { String label = Integer.toString(b); digit[b] = new Button(label); add(digit[b]); digit[b].addActionListener(this); } This code will create an array of buttons labelled 0 to 9. Converts the number b to a string Normal code for adding a button (A String is a collection of characters, note: the capital S for string (it's an Object not a simple variable))

8 PHY281 Scientific Java Programming ArraysSlide 8 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


Download ppt "PHY281 Scientific Java Programming ArraysSlide 1 Arrays In this section we will learn how about storing groups of related variables that share the same."

Similar presentations


Ads by Google