Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays.

Similar presentations


Presentation on theme: "Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays."— Presentation transcript:

1 Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays

2 Arrays We will only deal with arrays of primitive types in this course: – E.g. an array of ints or an array of booleans What is an array? – A collection of elements. – An array of ints is a collection of ints

3 Arrays (2) How to declare an array variable: int[] a; How to create the array itself using the new keyword: a = new int[3]; // creates an array for 3 ints This creates an array of 5 booleans: boolean[] b = new boolean[5];

4 Arrays (3) By default, all elements in the array will be 0, false or null (depending on the type) You can set the values in the array: a[0] = 30; a[1] = 40; a[2] = 50; Notice that the array index starts from 0! How to get values from the array: int x = a[2]; System.out.println(x); // will print out 50

5 Arrays (4) What is an ArrayIndexOutOfBoundsException? – When you try to use an index which is out of range – E.g.: setting the 4 th element of the array when there are only 3 elements: a[3] = 60; // index 3 refers to the 4 th element or retrieving an invalid element: int i = a[-2];

6 Arrays (5) Use.length to find out how many elements there are in an array: int []a = new int[15]; System.out.println ( a.length ); Can methods take in arrays and return arrays? public static int[] doSomething ( double[] x ){ … }

7 Try it Write a class which contains a main method which: – Declares & creates an array of 5 boolean values – Sets the values to true, false, true, false, true – Invokes the printArray method Write another method called printArray which: – Takes in an array of boolean values – Prints out the values in one row – Returns nothing

8 Arrays (6) “Lazy” way to initialize an array - the following 2 sections of code are identical: int []a = new int[3]; a[0] = 9; a[1] = 8; a[2] = 7; int []a= {9,8,7};

9 2D Arrays A 1D array can be viewed as a single row A 2D array is an array of arrays: – Can be viewed as a table of rows and columns Declare & create a 2D array of ints with 3 rows & 2 columns. int [][] a = new int[3][2];

10 2D Arrays (2) Setting values in a 2D array: a[0][0] = 3; a[0][1] = 9; Getting values from a 2D array: int x = a[2][1] ; Very flexible storage mechanism to represent a table of values

11 2D Arrays (3) a.length gives the number of rows a[0].length gives the number of columns in the 1 st row a[1].length gives the number of columns in the 2 nd row & so on

12 Example: getting the sum of all ints in a 2D array public class Test{ public static void main (String[] args){ int[][] a = new int [3][4]; // code to initialize values in a... int sum = 0; for (int i=0; i<a.length; i++){ for (int j=0; j<a[i].length; j++){ sum += a[i][j]; } System.out.println(sum); }

13 2D Arrays (4) “Lazy” way to initialize a 2D array: int [][]a = {{9,8},{1,7},{-1,2}}; 98 17 2

14 Summary Day1 : Introduction to the JDK, variables, operators Day 2 : looping Day 3 : decisions Day 4 : methods & 1D arrays Day 5: 2D arrays

15 Objectives achieved? To exercise your systematic thinking skills in logical problem solving To introduce you to Java To let you experience SMU’s learning culture Have you had fun?


Download ppt "Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays."

Similar presentations


Ads by Google