Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays. Last time on 4CS001 Anatomy of a Method int calculateArea(int w, int h) { int area = w * h; return area; } Parameters Used for passing data to.

Similar presentations


Presentation on theme: "Arrays. Last time on 4CS001 Anatomy of a Method int calculateArea(int w, int h) { int area = w * h; return area; } Parameters Used for passing data to."— Presentation transcript:

1 Arrays

2 Last time on 4CS001

3 Anatomy of a Method int calculateArea(int w, int h) { int area = w * h; return area; } Parameters Used for passing data to a method Return type Defines the type of data to be passed from the method. Keyword void is used here if methods returns no data. Return statement Used for passing data from the method. Omitted for void methods

4 Calling a Method int calculateArea(int w, int h) { int area = w * h; return area; } int area; area = calculateArea(2, 5); System.out.println("Area =" + area); Parameters values Are used to transfer data to a method Return value Contains the data passed from the method. Here it is copied into a variable A non-void method can be called anywhere an expression of the same type is permitted. e.g. from within calculations A call to a method called calculateArea

5 Arrays

6 Lecture Outcomes To understand the concept of arrays and their realisation in Java –Arrays of primitive data To understand the use of for loops for processing Arrays

7 An array is a structure that holds multiple values of the same type. –Allows us to use one variable name to access multiple items of data. The length of an array (the number of items it contains) is established when the array is created (at runtime). After creation, an array is a fixed-length structure. –Called a static data structure Arrays

8 A Summation Program The code below calculates the sum of 5 numbers. public class sumWithoutArray { public static void main (String args []) { int x0 = 7; int x1 = 9; int x2 = 6; int x3 = 5; int x4 = 3; int sum = x0 + x1 + x2 + x3 + x4; System.out.println("sum = " + sum); } What if we had 1000 numbers to sum?

9 Summation Using an Array public class sumWithArray1 { public static void main (String args []) { int [] x = new int[5]; int sum; x[0] = 7; x[1] = 9; x[2] = 6; x[3] = 5; x[4] = 3; sum = x[0] + x[1] + x[2] + x[3] + x[4]; System.out.println("sum = " + sum); } The x variable is an array reference. The array can store 5 integers. Each integer can be accessed with an index or subscript. The data type is int [ ] meaning integer array.

10 Using Arrays Before we can use an array we must create a reference to it and initialise it. int [] x = new int[5]; x[0] = 7; x[1] = 9; x[2] = 6; x[3] = 5; x[4] = 3; int [] x = {7, 9, 6, 5, 3}; 0 1 2 3 4 79653x x[2]

11 Assigning Arrays In the last case x was not an array, but a reference (or pointer) to an array. We can use array references just like any other references. int [] x = {7, 9, 6, 5, 3}; int [] y; y = x; System.out.println(y[1]); 9What is printed?

12 Summation With An Algorithm public class sumWithArray1 { public static void main (String args []) { int [] x = {7, 9, 6, 5, 3}; int i = 0; int sum = 0; while (i<5){ sum = sum + x[i]; i++; } System.out.println("sum = " + sum); } The x array can be initialised in one step A while loop can process all elements of the array. What if we had 1000 numbers to sum? Arrays allow us to process data algorithmically

13 for Loops for loops are preferable when the number of iterations is known in advance. int i; for(i=0; i<5; i++) { System.out.println(i); } loop control variable initialisation statement increment statement loop condition An alternative iterative construct to while loops. int i = 0; while (i<5){ System.out.println(i); i++; } Has the same effect as:

14 Using a for Loop with an array public class sumWithArray1 { public static void main (String args []) { int [] x = {7, 9, 6, 5, 3}; int i; int sum = 0; for(i=0; i<5; i++){ sum = sum + x[i]; } System.out.println("sum = " + sum); } for loops are particularly suitable for processing arrays - the length of the array is fixed, so it is known before the loop is entered. Earlier version had: int i = 0; while (i<5){ sum = sum + x[i]; i++; }

15 Summation With An Algorithm public class sumWithArray2 { public static void main (String args []) { int [] x = {7,9,6,5,3,1,2,3,4,5,9,1}; int i; int sum = 0; for(i=0; i<12; i++){ sum = sum + x[i]; } System.out.println("sum = " + sum); } The amount of data has changed. The only part of the program that needs to be changed is the loop control limit.

16 Summation Method public class sumWithArray3 { public static void main (String args []) { int [] x = {7,9,6,5,3,1,2,3,4,5,9,1}; int sum = summation(x); System.out.println("sum = " + sum); } We can pass arrays to methods just like we would pass any other references public int summation(int [] array) { int sum = 0; for(int i=0; i<12; i++) { sum = sum + array[i]; } return sum; } What would happen if the array only had 5 elements? Arrays are susceptible to ArrayIndexOutOfBoundsException

17 Summation Method public class sumWithArray4 { public static void main (String args []) { int [] x = {7, 9, 6, 5, 3}; int sum = summation(x, 5); System.out.println("sum = " + sum); } public int summation(int [] array, int nElements){ int sum = 0; for(int i=0; i<nElements; i++) { sum = sum + array[i]; } return sum; } We have alleviated the problem of the potential exception The method needs to know how many elements the array has. We can pass the number of elements as a parameter

18 The length attribute public class sumWithArray5 { public static void main (String args []) { int [] x = {7, 9, 6, 5, 3, 1, 2, 3, 4, 5, 9, 1}; int i; int sum = 0; for(i=0; i< x.length; i++){ sum = sum + x[i]; } System.out.println("sum = " + sum); } If we make use of the length attribute of the array the program becomes more robust. The length attribute of an array specifies how many elements the array may store Arrays are special – we can ask them how big they are…

19 Summation Method public class sumWithArray6 { public static void main (String args []) { int [] x = {7, 9, 6, 5, 3}; int sum = summation(x); System.out.println("sum = " + sum); } public int summation(int [] array){ int sum = 0; for(int i=0; i<array.length; i++) { sum = sum + array[i]; } return sum; } The method will work with arrays of any size The program is now more flexible and robust.

20 Example Program Write a program that lets the user type in 3 integers and store them in an array. The program should then print out the integers in reverse order. Assume the user enters 7, 4, 8

21 Example Program 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Example Program import javax.swing.JOptionPane; public class inputAndReverse { public static void main (String args []) { int [] x = new int[3]; int i; String inputNr; for(i=0; i<x.length; i++){ inputNr =JOptionPane.showInputDialog(null,"Enter number"); x[i] = Integer.parseInt(inputNr); } for(i=x.length-1; i>=0; i--){ System.out.println(x[i]); } Input from user: 7, 4, 8 During your workshop time do a dry run

22 Arrays of Other Types Arrays can be used with any of the primitive types char [] c = {'j','a','v','a',' ','i','s',' ', 'n','i','c','e'}; double [] values = new double[3]; values[0] = 3.1; values[1] = 3.2; values[2] = 7.5;

23 Arrays of Other Types We can also have Arrays of more complex types… String [] months = {"Jan", "Feb", "Mar"…"Dec"};

24 What is stored in the variable asparagus after this code is run? int [ ] white = {14, 29, 10, 22, 24, 28, 27, 19, 21, 30}; int asparagus = white[4]; White[4] equals 24

25 What is stored in the variable stomach after this code is run? int [ ] nose = {24, 17, 14, 13, 12, 20, 11, 10, 19, 23}; int [ ] orange = {0, 9, 5, 4, 6, 8, 7, 2, 1, 3}; int stomach = orange[6]; int hands = nose[stomach]; orange[6] equals 7 nose [7] equals 10

26 What is stored in the variable plum after this code is run? int [ ] strawberry = {25, 23, 14, 19, 13, 17, 29, 27, 10, 18}; int [ ] potato = {6, 8, 5, 9, 1, 7, 4, 3, 0, 2}; int grapefruit = potato[6]; int plum = strawberry[grapefruit]; Potato[6] equals 4 Strawberry [4] equals 13

27 What is stored in the variable toes after this code is run? int [ ] teeth = {15, 14, 12, 27, 19, 23, 10, 26, 17, 18}; int [ ] hands = {3, 6, 1, 5, 9, 8, 7, 0, 4, 2}; int toes = teeth[hands[5]]; hands[5] = 8 teeth[8] = 17

28 What is stored in the variable foot after this code is run? int [ ] shoulders = {25, 30, 21, 16, 18, 24}; int foot = shoulders.length; shoulders.length = 6

29 What is the largest index that could be used to access elements of lime? int [ ] lime = {21, 18, 29, 12, 26}; Largest index to access lime is 4 lime has five elements therefore its index has the possible values of zero through to 4

30 What is stored in the variable a after this code is run? int a = 26; int p; int [ ] x = {9, 17, 6, 18, 4, 1, 2, 16, 13, 14}; for(p=2; p<4; p=p+1) { a = a - x[p]; } a p p<4 x[p] 26 2 true 6 20 3 true 18 2 4 false a equals 2

31 What is stored in the variable blue after this code is run? int blue = 5; int knees; int [ ] greenPepper = {5, 17, 7, 2, 20, 15, 19, 6, 18, 16}; int [ ] white = {6, 2, 1, 0, 5, 8, 4, 9, 7, 3}; for(knees=4; knees<=6; knees=knees+1) { blue = blue + greenPepper[white[knees]]; } blue knees knees<=6 white[] greenPepper[] 5 4 true 5 15 20 5 true 8 18 38 6 true 4 20 58 7 false blue equals 58

32 What does this program output? public static void main(String[ ] args) { int [ ] banana = {7, 5, 3, 4, 1, 0, 8, 2, 6, 9}; String [ ] pineapple = { "skin", "purple", "strawberry", "persimmon", "fingers", "cabbage", "cucumber", "knees", "pumpkin", "turnip"}; String hair = broccoli(banana, pineapple); System.out.println(hair); } public static String broccoli(int [ ] lemon, String [ ] nose){ int peach = lemon[7]; return nose[peach]; } broccoli is called with arguments(banana, pineapple) banana is referred to as lemon pineapple is referred to as nose peach = lemon[7] = banana[7] = 2 nose[2]= pineapple[2]= "strawberry" Therefore hair equals "strawberry"

33 1-Dimensional Data So far we have only looked at 1-dimensional data (1D). –Suitable for linear lists of objects e.g. books in a library –Sequences of numbers or characters to be processed. –1D data can be stored in a 1D array and accessed with a single index. Multidimensional data is very common, so we need a means of storing it. We will cover this next week

34 Homework Spend 5 to 10 minutes reading through what we have just covered. –Ask questions in tutorial/workshop if you don’t understand something. Do the Quiz about arrays on Wolf (just questions 1 to 7 this week). Do workshop 9. Do the Jafa exercises.


Download ppt "Arrays. Last time on 4CS001 Anatomy of a Method int calculateArea(int w, int h) { int area = w * h; return area; } Parameters Used for passing data to."

Similar presentations


Ads by Google