problem solving using the java programming language 17-21 may 2010 mok heng ngee day 5: arrays
TRANSCRIPT
![Page 1: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/1.jpg)
Problem Solving using the Java Programming Language
17-21 May 2010Mok Heng Ngee
Day 5: Arrays
![Page 2: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/2.jpg)
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
![Page 3: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/3.jpg)
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];
![Page 4: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/4.jpg)
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
![Page 5: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/5.jpg)
Arrays (4)
• What is an ArrayIndexOutOfBoundsException?–When you try to use an index which is out
of range– E.g.: setting the 4th element of the array
when there are only 3 elements: a[3] = 60; // index 3 refers to the 4th elementor retrieving an invalid element: int i = a[-2];
![Page 6: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/6.jpg)
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 ){ …}
![Page 7: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/7.jpg)
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
![Page 8: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/8.jpg)
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};
![Page 9: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/9.jpg)
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];
![Page 10: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/10.jpg)
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
![Page 11: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/11.jpg)
2D Arrays (3)
• a.length gives the number of rows• a[0].length gives the number of columns in
the 1st row• a[1].length gives the number of columns in
the 2nd row & so on
![Page 12: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/12.jpg)
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);
}
}
![Page 13: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/13.jpg)
2D Arrays (4)
• “Lazy” way to initialize a 2D array:
int [][]a = {{9,8},{1,7},{-1,2}};
9 81 7-1 2
![Page 14: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/14.jpg)
Summary
• Day1 : Introduction to the JDK, variables, operators
• Day 2 : looping• Day 3 : decisions• Day 4 : methods & 1D arrays• Day 5: 2D arrays
![Page 15: Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays](https://reader036.vdocuments.us/reader036/viewer/2022072015/56649ed85503460f94be6c25/html5/thumbnails/15.jpg)
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?