Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays Wellesley College CS230 Lecture 02 Thursday, February 1

Similar presentations


Presentation on theme: "Arrays Wellesley College CS230 Lecture 02 Thursday, February 1"— Presentation transcript:

1 Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
Handout #9 Reading: Downey: Chapter 10 Problem Set: Assignment #1 due Tuesday, February 13

2 Array Operations int[] 9 1 7 2 1 3 8 4 6 a Java arrays are homogeneous: all slots must contain the same kind of element. E.g., an array with type int[] is an array of integers. Java arrays are fixed length: a.length → 5 Get the contents of a slot via a[index] (“a sub index”): a[0] → 9, a[4] → 6, a[5] → ArrayIndexOutOfBoundsException Set the contents of a slot via a[index]= expression; a[0] = 2; a[1] = a[2] + a[3]; a[i]++ means a[i] = a[i] + 1; and a[i]-- means a[i] = a[i] – 1; Create an integer array with num slots via new int[num]: int [] a = new int[5]; // All slots contain 0 by default a[0]=9; a[1]=7; … ; a[4]=6; Shorthands: int [] a = new int[] {9,7,1,8,6}; int [] a = {9,7,1,8,6};

3 Arrays Elements can be of Any Type
boolean[] b1 = new boolean[4]; boolean[] b2 = {true,false,true}; boolean[] boolean[] false 1 false 2 false 3 false true 1 false 2 true b1 b2 char[] ‘c’ 1 ‘s’ 2 ‘2’ 3 ‘3’ 4 ‘0’ char[] c = {’c’,’s’,’2’,’3’,’0’}; c String[] s1 = new String[3]; String[] s2 = {“I”, “am” “Sam”}; String[] String[] 1 2 1 2 s2 s1 “I” “am” “Sam”

4 Arrays of Arrays An irregular 2-dimensional array A regular 2D array
int[][] aa1 = {{9,7,1,8,6}, {4,0}, {}, {5,2,3}}; int[][] aa2 = {{5,2,7,6}, {9,0,8,4}, {7,3,2,5}}; int[][] int[] aa2 1 7 2 1 3 8 4 6 int[][] int[] 9 1 2 2 7 3 6 int[] 5 4 1 int[] 1 1 2 8 3 4 1 aa1 int[] 9 2 int[] 1 3 2 3 5 2 int[] 7 5 1 2 2 3 3 OR int[][] aa2 = new int[3][4]; aa2[0][0] = 5; aa2[0][1] = 2; …

5 Arrays Diagrams Illustrate Sharing
int[][] int[] 1 7 2 1 3 8 4 6 aa1[3] = aa1[0]; aa1[0][2] = aa1[0][3] + aa1[0][4]; aa1[3][0] = aa1[3][1] + aa1[3][2]; System.out.println(aa1[3][0]); 9 int[] 4 1 1 aa1 int[] 2 int[] 5 1 2 2 3 3

6 A Simple Array Iteration: Summation
int[] Iteration Table 9 1 7 2 1 3 8 4 6 nums i ans 1 9 2 16 3 17 4 25 5 31 Want IntArray.sum(nums) → 31 // Assume this is in the class IntArray public static int sum (int [] a) { }

7 What does mystery() do? nums IntArray.mystery(nums) → ???
9 1 7 2 1 3 8 4 6 nums IntArray.mystery(nums) → ??? // Assume this is in the class IntArray public static String mystery (int [] a) { if (a.length == 0) // The empty array is a special case return "{}"; else { String result = "{" + a[0]; for (int i = 1; i < a.length; i++) { // Why is the first index 1, not 0? result = result + "," + a[i]; } return result + "}";

8 Testing sum(): Approach 1
Suppose we want java IntArray sum to give the following output: sum({9,7,1,8,6}) = sum({4,0}) = sum({}) = sum({5,2,3}) = 10 // Assume this is in the class IntArray public static void testSum (int [] a) { System.out.println("sum(" + toString(a) + ") = " + sum(a)); } public static void main (String [] args) { if (args.length == 1) { if (args[0].equals(“sum”)) { testSum(new int[] {9,7,1,8,6}); testSum(new int[] {4,0}); testSum(new int[] {}); testSum(new int[] {5,2,3}); } else … // Testing of other methods goes here } else System.out.println(“usage: IntArray <name>”);

9 Testing sum(): Approach 2
public static void main (String [] args) { int[][] arrays = {{9,7,1,8,6}, {4,0}, {}, {5,2,3}}; if (args.length == 1) { if (args[0].equals(“sum”)) { } else … // Testing of other methods goes here } else System.out.println(“usage: IntArray <name>”); }

10 Testing sum(): Approach 3
Suppose we want java IntArray sum to give the following output: sum({8,17,1,22,6}) = 54 // Assume this is in the class IntArray // Returns an integer array based on strings in strs[1..(strs.length-1)] public static int[] makeIntArray (String[] strs) { } public static void main (String [] args) { if (args.length >= 1) { // Use >= to allow extra arguments if (args[0].equals(“sum”)) { testSum(makeIntArray(args)); } else … // Testing of other methods goes here } else System.out.println(“usage: IntArray <name> <int1> … <intn> ”);

11 Array Reversal Before: nums After: nums
int[] 9 1 7 2 1 3 8 4 6 Before: nums IntArray.reverse(nums); int[] 6 1 8 2 1 3 7 4 9 After: nums // Assume this is in the class IntArray public static void reverse (int [] a) { }

12 Copying Array Reversal
int[] 9 1 7 2 1 3 8 4 6 Before: nums int[] c = IntArray.copyReverse(nums); After: int[] int[] 9 1 7 2 1 3 8 4 6 6 1 8 2 1 3 7 4 9 nums c // Assume this is in the class IntArray public static int[] copyReverse (int [] a) { }

13 Column Summation Want IntArray.sumColumns(aa2) → {21, 5, 17, 15}
(assume input is a regular 2D array) 1 2 2 7 3 6 5 int[] 1 2 8 3 4 aa2 1 9 int[] 1 3 2 3 5 2 7

14 Insertion Sort 9 1 7 2 1 3 8 4 6 7 1 9 2 1 3 8 4 6 1 1 7 2 9 3 8 4 6 1 1 7 2 8 3 9 4 6 1 1 6 2 7 3 8 4 9


Download ppt "Arrays Wellesley College CS230 Lecture 02 Thursday, February 1"

Similar presentations


Ads by Google