spring 2008 mark fontenot cse 1341 principles of computer science i note set 7
Post on 06-Jan-2018
214 Views
Preview:
DESCRIPTION
TRANSCRIPT
Spring 2008
Mark Fontenotmfonten@engr.smu.edu
CSE 1341Principles of Computer Science I
Note Set 7
Note Set 7 Overview
ArraysDeclaringAccessingUsing the enhanced for loopPassing Arrays to Methods
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
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
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
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
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
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
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
ExerciseGiven:
int [] x = {40, 99, 100, 88, 28, 32, 101, 89};
Write a snippet of code to print out all elements that are even.
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.
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?
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;}
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.
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
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]; //???
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(); }
}
top related