Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.

Similar presentations


Presentation on theme: "Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array."— Presentation transcript:

1 Lecture 7: Arrays Tami Meredith

2 Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array

3 What is data? Data are values that are stored in memory Memory locations are given names Those names are called identifiers A named location is called a variable A type describes the contents of the memory location in general terms so we can make the location the correct size Thus, we have: 1. Memory locations – of some size 2. Types – to tell us things about the value we will store 3. Names – to identify the locations 4. Values – to put in the locations

4 Variables 1. int i; Defines a section of memory large enough to hold an integer and names that area of memory " i ", the memory contains a random value from whatever it was used for previously 2. i = 5; Stores the value 5 in the area or memory named " i " 3. int i = 5; Combines 1. and 2. A variable holds one, and only one, thing A variable always has a value... if you put nothing there then assume it has a random value

5 Groups of Variables A block lets us group statements so that they can be used where a single statement is needed An array lets us group variables so that we can store groups of similar values with a single name An array is a set of adjacent (side by side) memory locations We use square brackets, " [ " and " ] " when working with arrays

6 Aside: Delimiters [ ] Something to do with an array " " A string (sequence of zero or more characters) 'a' A character (a single character, never empty) { } A block (sequence of zero or more statements) /* */ A multi-line comment ( ) A math expression A test in a loop or conditional The parameters of a method call The parameters in a method declaration

7 Arrays 4 integers stored (somewhere) in memory int a, b, c, d; An array named " e " that stores 4 integers int[] e = new int[4]; a dcb e[0]e[1]e[2]e[3]

8 Array Syntax: Definition int[] e = new int[4]; type [] name = new type [ number-of-elements ]; type tells us how big each slot needs to be and what it will hold name is what the entire array is called new is a special keyword that means "assign memory" and is always followed by an indicator of how much memory to get (in this case, enough for 4 integers)

9 Array Syntax: Assignment int[] coords = new int[2]; coords[0] = 3; coords[1] = 5; 0 and 1 are referred to as array indices Arrays are indexed from 0 because the index identifies how many slots we are from the front of the array An index is not a count into the array! (Its actually an offset) Same as Strings (c.f., arrays of characters) coords[0]coords[1] 35

10 new 1. int x; 2. x = 4; 1. int[] y; 2. y = new int[4]; 3. y[0] = 1; y[1] = 2; y[2] = 4; y[3] = 8; Arrays add the extra step of indicating how many values the array needs to store (and getting the memory for it at that time using new ). x 4 y[0]y[1]y[2]y[3] 1248

11 new (Part 2) 1. int[] y; 2. y = new int[4]; Step 1 creates a memory slot called y Step 2 creates 4 memory slots called y[0], y[1], y[2], a nd y[3] y stores a. Information to access y[0] to y[3] b. A count of then number of slots created y is secretly an object, it stores a group of data

12 Indices Strings are indexed from 0 Arrays are indexed from 0 We often have called the counter in a loop as the index value We do this because we frequently use loops to process all the elements (i.e., the value at each index) of arrays and Strings Really, a String is just an array of characters with some special rules

13 Indices We can use a variable to indicate an index value int x = 0, y = 1; int[] coords = new int[2]; coords[x] = 3; // same as coords[0] coords[y] = 5; // same as coords[1]

14 Arrays and Loops Arrays work amazingly well with loops! Let us see how to get four integers from a user... int[] vals = new int[4]; int i; Scanner user = new Scanner(System.in); for (i = 0; i <= 4; i++) { vals[i] = user.nextInt(); } /* * Same as: vals[0] = user.nextInt(); * vals[1] = user.nextInt(); * vals[2] = user.nextInt(); * vals[3] = user.nextInt(); */

15 Exercise Given the array below, write a loop to fill the array with the values 0 to 9 int[] vals = new int[10]; // your loop...

16 Solution Given the array below, write a loop to fill the array with the values 0 to 9 (see arrayfill.java) int[] vals = new int[10]; int i; // i is traditionally an array index for (i = 0; i < 10; i++) { vals[i] = i; }

17 Improving our solution Arrays have their length stored Rather than write code that is based on some value that could change, we can tie the loop to the array so that if the array changes, the loop changes int[] vals = new int[10]; int i; for (i = 0; i < vals.length; i++) { vals[i] = i; }

18 Length For any array called name, name.length returns the length of the array The length is the number of memory slots available, NOT the number of memory slots that have been assigned values int[] vals = new int[4]; vals[0] = vals.length; vals[0] holds the value 4 even though vals[1] to vals[3] have not been used

19 A tiny quirk Arrays store their length in a variable named length We directly access the variable to obtain the length of the array Strings store their length in a hidden (private) variable We use the length() method to obtain the length of the String This is why we need () with Strings and not arrays Both Strings and arrays are actually objects

20 A Problem int sum (int[] data) { int i, sum = 0; for (i = 0; i < data.length; i++) { sum = sum + data[i]; } return(sum); } Problem... what happens when data isn't filled? That is, what if some of the locations do not contain valid values? E.g., data[i] does not have a value?

21 Solution1 Keep track of the number of used locations in a separate variable (e.g., size in this example) Can waste space and requires extra data management int sum (int[] data, int size) { int i, sum = 0; for (i = 0; i < size; i++) { sum = sum + data[i]; } return(sum); }

22 Solution2 Resize the array so that no empty locations exist Very inefficient, not supported in Java Must make a new array of the correct (smaller) size and copy values to it from the old array int size; // number of values used in data int[] dataCopy = new int[size]; int i; for (i = 0; i < size; i++) { dataCopy[i] = data[i]; } Now we can use dataCopy in place of data and dataCopy.length is correct – length and size(number of used elements) is the same

23 Powers of 2: An example powers[n] = 2 n powers = 1, 2, 4, 8, 16,..., 2 31 int[] powers = new int[32]; int i; powers[0] = 1; // 2^0 = 1 for (i = 1; i < 32; i++) { powers[i] = 2 * powers[i-1]; }

24 More Improvements Literals are generally a bad thing Named constants are seen as better More readable Only defined once – change the definition and you change all the uses of the constant Named constants usually have their NAME IN CAPS

25 Example public class powers { public static final int COUNT = 32; public static void main (String[] args) { int[] pow2 = new int[COUNT]; int i; pow2[0] = 1; // 2^0 = 1 for (i = 1; i < COUNT; i++) { pow2[i] = pow2[i-1] + pow2[i-1]; } } // end main() } // end class powers

26 Classes and Static Classes group methods and data In class powers, we had a method, main, and some data, count stati c means there is only one copy – we need the class to only have one copy of main because we can't have two starting points! since main is static, it can only use other (external) things that are static – that is why COUNT is static as well

27 Loop Variables Often, we define a variable only to use within loops We can actually "hide" this variable inside the loop by defining it there for (int i = 0; i < data.length; i++) { // i can only be used here } // i cannot be used here (outside the loop) i can only be used inside the loop since it is defined inside the loop i does not exist before, or after, the loop

28 Slightly Shorter Example public class powers { public static final int COUNT = 8; public static void main (String[] args) { int[] pow2 = new int[COUNT]; pow2[0] = 1; // 2^0 = 1 for (int i = 1; i < COUNT; i++) { pow2[i] = pow2[i-1] + pow2[i-1]; } } // end main() } // end class powers

29 Initialisation We can initialise an array by assigning it a value int[] colour = { 255, 0, 0 }; Automatically calculates the size we need based on the value Uses curly braces { and }, not brackets [ and ] Think of the initialiser as a "block of values"

30 Exercise The array hours = {3, 5, 9, 7, 6} stores the number of hours that Julie worked for the 5 days of week. If Julie makes $12.50 an hour, write a program to: 1. Calculate and store in an array, the number of $ Julie made each day. 2. Calculate and print her average daily income.

31 Solution public class income { public static final double SALARY = 12.50; public static final int DAYSOFWEEK = 5; public static void main (String[] args) { int[] hours = {3, 5, 9, 7, 6}; double[] earned = new double[DAYSOFWEEK]; double sum = 0.0; for (int i = 0; i < DAYSOFWEEK; i++) { earned[i] = ((double) hours[i]) * SALARY; sum += earned[i]; } System.out.println("Average daily income: " + (sum/((double) DAYSOFWEEK))); } // end main() } // end class income

32 Exercise A tic-tac-toe board has rows and columns Any square on the board can be described as (row, col) Here is the output where all the rows and columns are identified: (1,1), (1,2), (1,3) (2,1), (2,2), (2,3) (3,1), (3,2), (3,3) Write a program using nested loops (one for the rows and one for the columns) that will generate this output

33 Solution public class tictactoe { public static void main (String[] args) { for (int r = 1; r < 4; r++) { for (int c = 1; c < 4; c++) { System.out.print("("+r+","+c+")"); if (c < 3) System.out.print(", "); } // end c loop System.out.println(""); } // end r loop } // end main() } // end class tictactoe

34 Two Dimensions Each row on the board could be an array Thus, we could make the board as an array of arrays E.g., one array of length 3, each slot holding another array of length 3 (the rows) One array that holds 3 arrays! 0holds012 1 012 2 012

35 2 Dimensional Arrays int rows = 3; int cols = 3; int[][] board = new int[rows][cols]; board[0][0]board[0][1]board[0][2] board[1][0]board[1][1]board[1][2] board[2][0]board[2][1]board[2][2]

36 Accessing with Loops int rows = 3; int cols = 3; int val = 1; int[][] board = new int[rows][cols]; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { board[r][c] = val++; } board[0][0] = 1board[0][1] = 2board[0][2] = 3 board[1][0] = 4board[1][1] = 5board[1][2] = 6 board[2][0] = 7board[2][1] = 8board[2][2] = 9

37 Another 2 Dimensional Array int rows = 4; int cols = 2; int[][] board = new int[rows][cols]; board[0][0]board[0][1] board[1][0]board[1][1] board[2][0]board[2][1] board[3][0]board[3][1]

38 Sorting It is convenient to be able to sort an array There is a provided set of methods that sort an array for us These methods are static (rely on no data except that which is given to them) and thus no object needs to be instantiated java.util.Arrays (data); will sort the array named data Works on integers, strings, floats (anything that can be ordered)

39 Example import java.util.Arrays; public static void fill (int[] data) { for (int i = data.length - 1; i >= 0; i--) { data[i] = i; } Arrays.sort(data); }

40 Now What? No more additional chapters!! Now we go back and start: 1. Reviewing 2. Covering things we skipped 3. Consolidating concepts Now we can slow down and enjoy the scenery!

41 To Do Go to the lab and complete Assignment 6 Read Chapter 7 if you have not done so already! Go back and start re-reading Chapters 1-6 until you *REALLY* understand the material in them Practice, Practice, Practice! Program until your eyeballs fall out! (It is covered under your student health plan, I checked).


Download ppt "Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array."

Similar presentations


Ads by Google