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

17
Spring 2008 Mark Fontenot [email protected] CSE 1341 Principles of Computer Science I Note Set 7

description

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

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

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

Spring 2008

Mark [email protected]

CSE 1341Principles of Computer Science I

Note Set 7

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

Note Set 7 Overview

ArraysDeclaringAccessingUsing the enhanced for loopPassing Arrays to Methods

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

ArraysFixed LengthHomogenous typeelements are somehow related usually

An array is a reference typean array name is a reference variable even though the array

holds primitive-typed data.

Arrays are zero subscripted

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

Declarations

double[] temperatures;// or double temperatures[];

temperatures = new double[20];

no doubles yet

Now we have 20doubles.

int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Initializer List – Array just big enough is allocatedand these values are copied into the array

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

Enhanced Forpublic 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 = 20Val = 30Val = 40Val = 50

called theparameter

Page 6: Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 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());

3, 4

5, 6

7, 8

p

x

Page 7: Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 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

Page 8: Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 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

10, 10

7, 8

p

x

Page 9: Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 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

10, 10

10, 10

p

x

Can’t do anything fancy with the enhanced for…• no skipping elements

A copy of value/reference placed in parameter

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

ExerciseGiven:

int [] x = {40, 99, 100, 88, 28, 32, 101, 89};

Write a snippet of code to print out all elements that are even.

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

ExerciseGiven:

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.

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

Arrays and MethodsPassing 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?

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

Return an array from a methodYou 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 < bpublic 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;}

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

Write Some CodeWrite 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.

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

Two Dimensional ArraysSimilar to a table of values with rows and columnsThink of it as an array of 1-D arrays – each row is an array

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

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 columnsin that row

int[][] x = new int [2];

x[0] = new int[10]; //???

x[1] = new int[20]; //???

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

Printing elements from arraypublic 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(); }

}