Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.

Similar presentations


Presentation on theme: "Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7."— Presentation transcript:

1 Spring 2008 Mark Fontenot mfonten@engr.smu.edu CSE 1341 Principles of Computer Science I Note Set 7

2 Note Set 7 Overview Arrays Declaring Accessing Using the enhanced for loop Passing Arrays to Methods

3 Arrays Fixed Length Homogenous type elements are somehow related usually An array is a reference type an array name is a reference variable even though the array holds primitive-typed data. Arrays are zero subscripted

4 Declarations double[] temperatures; // or double temperatures[]; temperatures = new double[20]; no doubles yet Now we have 20 doubles. int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Initializer List – Array just big enough is allocated and these values are copied into the array

5 Enhanced For public class ArrayOne { public static void main (String[] args) { int[] tests = {10, 20, 30, 40, 50}; for(int x: tests) { System.out.printf("Val = %d\n", x); } Val = 10 Val = 20 Val = 30 Val = 40 Val = 50 Val = 10 Val = 20 Val = 30 Val = 40 Val = 50 called the parameter

6 Enhanced For Example Point[] p = new Point[3]; p[0] = new Point(3, 4); p[1] = new Point(5, 6); p[2] = new Point(7, 8); for(Point x:p) x.setLocation(10, 10); for(Point z: p) System.out.println(z.toString()); 3, 4 5, 6 7, 8 p x

7 Enhanced For Example Point[] p = new Point[3]; p[0] = new Point(3, 4); p[1] = new Point(5, 6); p[2] = new Point(7, 8); for(Point x:p) x.setLocation(10, 10); for(Point z: p) System.out.println(z.toString()); 10, 10 5, 6 7, 8 p x

8 Enhanced For Example Point[] p = new Point[3]; p[0] = new Point(3, 4); p[1] = new Point(5, 6); p[2] = new Point(7, 8); for(Point x:p) x.setLocation(10, 10); for(Point z: p) System.out.println(z.toString()); 10, 10 7, 8 p x

9 Enhanced For Example Point[] p = new Point[3]; p[0] = new Point(3, 4); p[1] = new Point(5, 6); p[2] = new Point(7, 8); for(Point x:p) x.setLocation(10, 10); for(Point z: p) System.out.println(z.toString()); 10, 10 p x Can’t do anything fancy with the enhanced for… no skipping elements A copy of value/reference placed in parameter

10 Exercise Given: int [] x = {40, 99, 100, 88, 28, 32, 101, 89}; Write a snippet of code to print out all elements that are even.

11 Exercise Given: int [] x = {40, 98, 100, 88, 28, 32, 108, 89}; Write a snippet of code to print out all elements that end in the number 8.

12 Arrays and Methods Passing an array to a method public static void main (…) { int[] myArr = {1, 2, 3, 4, 5}; System.out.println(sumElements(myArr)); } public static int sumElements(int[] arr) { int sum = 0; for(int x : arr ) sum += x; return sum; } If we modified arr, would the changes be reflected in myArr also?

13 Return an array from a method You can return an array reference from a method public static void main (…) { int[] myArr = createArray(10, 50); for (int x : myArr) System.out.println(x); } //Creates array filled with ints between //the two args, assuming that a < b public static int createArray(int a, int b) { int[] arr = new int [b – a + 1]; for (int i = a; i <= b; i++) arr[i – a] = i; }

14 Write Some Code Write a static method that accepts an integer array and returns an array holding any integer between 0 and 10 that is not stored in the parameter array.

15 Two Dimensional Arrays Similar to a table of values with rows and columns Think of it as an array of 1-D arrays – each row is an array

16 Declaring a 2 D array int[][] x = new int [10][20]; for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) x[i][j] = 10; Number of Rows Number of columns in that row int[][] x = new int [2]; x[0] = new int[10]; //??? x[1] = new int[20]; //???

17 Printing elements from array public static void main (String [] args) { int[][] array1 = {{1, 2, 3}, {4, 5, 6}}; int[][] array2 = {{1, 2}, {3}, {4, 5, 6}}; outputArray (array1); outputArray (array2); } public static void outputArray(int[][]a) { for (int row = 0; row < a.lenght; row++) { for (int col = 0; col < a[row].length; col++) System.out.printf(“%d “, a[row][col]); System.out.println(); }


Download ppt "Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7."

Similar presentations


Ads by Google