Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays CS140: Introduction to Computing 1 Savitch Chapter 7 10/23/13 10/28/13.

Similar presentations


Presentation on theme: "Arrays CS140: Introduction to Computing 1 Savitch Chapter 7 10/23/13 10/28/13."— Presentation transcript:

1 Arrays CS140: Introduction to Computing 1 Savitch Chapter 7 10/23/13 10/28/13

2 2 01 2 3 4 5 index  int  60-3124 2 34 -666 0

3 Array – a new kind of variable An array is a collection of variables of the same type (primitive or class type) 3 VARIABLE one name one place to store one value of one type ARRAY one name multiple places multiple values of one type Used when the solution to a problem requires a list of values to be processed

4 Declare an array Type[] name = new Type[size]; Examples: int[] qGrade = new int[10]; //10 quiz grades String[] name = new String[20]; //20 names Ball[] ball = new Ball[15]; //15 Ball objects

5 5 01 2 3 4 5 index  int  60-3124 2 34 -666 int[ ] intTrain = new int[6]; intTrain[0] = 60; intTrain[1] = -3; intTrain[2] = 124; intTrain[3] = 2; intTrain[4] = 34; intTrain[5] = -666; all of these can be used as variables of type int ARRAY

6 Array example double[] temperature = new double[3]; temperature[0] = 38; temperature[1] = temperature[0] + 6; temperature[6%4] = 60.3; int length = temperature.length; 6 3 3 012 38 012 44 012 384460.3 012

7 Array example public static final int NUMBER_OF_STUDENTS = 10; //array to hold ten quiz grades int[] qGrade = new int[NUMBER_OF_STUDENTS ]; The first location is index 0 qGrade[0] = 80; sum += qGrade[0]; 80 0123456789

8 Array example int[] hTemp = new int[7]; // seven high temps. The last location is index (size-1) hTemp[6] = 45; … sum += hTemp[6]; 45 0123456

9 Array length int[] hTemp = new int[7]; // seven high temps. System.out.println(“length: “+ hTemp.length); 0123456 7 7 How many places, not how many values!

10 Array length Arrays are a kind of object Arrays have only one public instance variable – length length is also final, so int[] hTemp = new int[7]; hTemp.length = 10; hTemp[7] is out of bounds First array value is in hTemp[0] Last array value is in hTemp[hTemp.length – 1] Make sure your indices stay within range [0 – (length-1)] is illegal!! it will compile but cause an error at runtime

11 Array initialization int[] chapters = {1, 2, 3, 4, 5, 6, 10, 7}; 123456107 0 1 2 3 4 5 6 7

12 Array + looping int[] hTemp = new int[7]; // seven high temps. for(int i = 0; i < hTemp.length; i++){ System.out.println(i + ": " + hTemp[i]); } 0123456 0: 0 1: 0 2: 0 3: 0 4: 0 5: 0 6: 0 0: 0 1: 0 2: 0 3: 0 4: 0 5: 0 6: 0

13 Array + looping int[] hTemp = {70, 69, 31, 30, 52, 48, 50}; int sum = 0; for (int temp=0; temp < hTemp.length; temp++){ sum += hTemp[temp]; } System.out.println(sum); System.out.println(sum/ hTemp.length); 70693130524850 0123456 350 50 350 50

14 Arrays int[] daysMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 031283130313031 30313031 0123456789101112

15 Arrays 15

16 public class Date { private int day; private int month; private int year; private static int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; public void setMonth( int month ){ this.month = month; } //validate day value public void setDay( int day ) { if( day daysInMonth[ month ] ) { System.out.println( "bad value in Date.setDay()" ); System.exit(2); } this.day = day; } public void showDate() { System.out.println( this.month + "/" + this.day ); } 16 public class Test { public static void main(String[] args) { Date d_1 = new Date(); d_1.setMonth(2); d_1.setDay(29); d_1.showDate(); } bad value in Date.setDay() Java Result: 2 bad value in Date.setDay() Java Result: 2

17 Naming convention Array names are generally singular testScore[3] entry[5] 17

18 Arrays (what can I do?) Assign an individual value to one place Read/get an individual value from one place Read/get the length of the array (how many places, not how many values)

19 Arrays (what can I NOT do?) Assign values to multiple places at once Array multiplication or addition Compare two arrays: if(a[] == b[]) Assign one array with another a[] = b[];

20 Array usage: to keep a list A list of values (all of the same type) – A list of names – A list of test scores – A list of rainfall amounts – A list of objects – …..

21 So far.. Chapter 7.1 21

22 Arrays in Classes and Methods Arrays can be used with classes and methods like any other object 22

23 public class Wizard { private String name; private String skill; public Wizard() { this.name = ""; this.skill = ""; } public Wizard( String name, String skill ) { this.name = name; this.skill = skill; } public void setSkill( String newSkill ) { skill = newSkill; } public String getWizard() { return name + " - skill: " + skill; } 23 public class Test2 { public static void main(String[] args) { Wizard[] wiZzard = new Wizard[3]; Wizard w_1 = new Wizard( "Rincewind", "none, escape" ); Wizard w_2 = new Wizard( "The Luggage", "follow owner everywhere, be vicious, defend owner" ); Wizard w_3 = new Wizard( "Death", "death, drink without getting drunk, speak in small caps" ); wiZzard[0] = w_1; wiZzard[1] = w_2; wiZzard[2] = w_3; for (int i=0; i<wiZzard.length; i++) System.out.println( wiZzard[i].getWizard() ); } Rincewind - skill: none, escape The Luggage - skill: follow owner everywhere, be vicious, defend owner Death - skill: death, drink without getting drunk, speak in small caps Rincewind - skill: none, escape The Luggage - skill: follow owner everywhere, be vicious, defend owner Death - skill: death, drink without getting drunk, speak in small caps

24 Arrays as parameters/arguments 24 modifier return_type method_name ( base_type[] param_name ) { … }

25 public class Wizard { private String name; private String skill; public Wizard() { this.name = ""; this.skill = ""; } public Wizard( String name, String skill ) { this.name = name; this.skill = skill; } public void setSkill( String newSkill ) { skill = newSkill; } public String getWizard() { return name + " - skill: " + skill; } public static void printWizardList(Wizard[] wiz) { for (int i = 0; i < wiz.length; i++ ) System.out.println( wiz[i].getWizard() ); } 25 public class Test2 { public static void main(String[] args) { Wizard[] wiZzard = new Wizard[3]; Wizard w_1 = new Wizard( "Rincewind", "none, escape" ); Wizard w_2 = new Wizard( "The Luggage", "follow owner everywhere, be vicious, defend owner" ); Wizard w_3 = new Wizard( "Death", "death, drink without getting drunk, speak in small caps" ); wiZzard[0] = w_1; wiZzard[1] = w_2; wiZzard[2] = w_3; Wizard.printWizardList(wiZzard); } Rincewind - skill: none, escape The Luggage - skill: follow owner everywhere, be vicious, defend owner Death - skill: death, drink without getting drunk, speak in small caps Rincewind - skill: none, escape The Luggage - skill: follow owner everywhere, be vicious, defend owner Death - skill: death, drink without getting drunk, speak in small caps empty square brackets in param list no square brackets in argument list

26 Arrays in Classes and Methods public void up( int[] a ) { for ( int i = 0; i < a.length; i++ ) a[i] = a[i] + 1; } 26 int[] a = {1, 2, 3, 4}; up(a); System.out.println(a[0]); System.out.println(a[1]); System.out.println(a[2]); System.out.println(a[3]); 23452345 23452345

27 Array Assignment and Equality int[] b = {0, 1, 2, 3}; int[] c = new int[4]; int[] d = {11, 22, 33, 44, 55}; System.out.println( c[2] ); c = b; System.out.println( c[2] ); d[1] = b[3]; System.out.println(d[1]); System.out.println(d[0]); 27 0 2 The entire contents of the array are stored in the same location in memory '' z char[] f = {'a', 'z', 'f', 'g'}; char[] e = new char[4]; System.out.println( e[1] ); e = f; System.out.println( e[1] ); System.out.println( e==f ); char[] g = {'a', 'z', 'f', 'g'}; System.out.println( f==g ); 3 11 true false

28 public class ArrayTest{ public static void main(String[] args) { int[] aa = {1, 2, 3, 4}; int[] bb = {1, 2, 3, 5}; int[] cc = {1, 2, 3, 4}; System.out.println( equals( aa, bb ) ); System.out.println( equals( aa, cc ) ); System.out.println( aa == cc ); } public static boolean equals( int[] a, int[] b ) { if ( a.length != b.length ) return false; else { for ( int i = 0; i < a.length; i++ ) if ( a[i] != b[i] ) return false; return true; } 28 false true false public static boolean equals( int[] a, int[] b ) { boolean elementsMatch = true; if ( a.length != b.length ) elementsMatch = false; else { int i = 0; while( elementsMatch && ( i < a.length) ) { if ( a[i] != b[i] ) elementsMatch = false; i++; } return elementsMatch; }

29 Arrays as return types 29 modifier base_type[] method_name ( base_type[] param_name ) { … }

30 Arrays as return types 30

31 Arrays as return types String str = “foot"; char[] strToChar = str.toCharArray(); for (int i = 0; i < strToChar.length; i++ ) System.out.println( strToChar[i] ); 31 footfoot footfoot

32 public class ArrayTest{ public static void main(String[] args) { String doubleMe = "owl"; char[] doubled = doubleString( doubleMe ); for ( int i = 0; i < doubled.length; i++ ) System.out.println( doubled[i] ); } public static char[] doubleString( String s ) { s = s + s; char[] result = s.toUpperCase().toCharArray(); return result; } 32 OWLOWLOWLOWL OWLOWLOWLOWL

33 Dynamic Array Size Scanner keyboard = new Scanner( System.in ); System.out.print("What is the size of the array?"); int arraySize = keyboard.nextInt(); int[] number = new int[arraySize]; 33

34 Scanner keyboard = new Scanner( System.in ); System.out.println("What is the size of the array?"); int arraySize = keyboard.nextInt(); int[] number = new int[arraySize]; number[5] = 6; 34 What is the size of the array? 4 What is the size of the array? 4 0 1 2 3 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at test2.Test2.main(Test2.java:49) Java Result: 1 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at test2.Test2.main(Test2.java:49) Java Result: 1 Dynamic Array Size

35 Fibonacci int[] fibonacci = new int[15]; fibonacci[0] = 0; fibonacci[1] = 1; for (int i = 2; i < 15; i++) fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]; for (int j = 0; j < 15; j++) System.out.print( fibonacci[j] + " " ); 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 35 RECURSION

36 So far.. 7.1, 7.2 and 7.3 Next 7.4 – searching and sorting 7.5 – multidimensional array fun 36

37 Sorting Arrays - Selection Sort 16 5 22 45 2 11 3 0 1 2 3 4 5 6 16 5 22 45 2 11 3 16 5 22 45 2 11 3 2 5 22 45 16 11 3 2 5 22 45 16 11 3 2 3 22 45 16 11 5 2 3 22 45 16 11 5 2 3 5 45 16 11 22 2 3 5 45 16 11 22 2 3 5 11 16 45 22 2 3 5 11 16 45 22 2 3 5 11 16 45 22 2 3 5 11 16 45 22 2 3 5 11 16 22 45

38 38 public class Test { public static void main (String[] args) { double[] list = { 1, 9, 4.5, 6.6, 5.7, -4.5 }; selectionSort(list); for ( int i = 0; i < list.length; i++ ) System.out.print( list[i] + " " ); } public static void selectionSort( double[] list ) { for ( int i = 0; i < list.length - 1; i++ ) { double currentMin = list[i]; int currentMinIndex = i; for ( int j = i + 1; j < list.length; j++ ) { if ( currentMin > list[j] ) { currentMin = list[j]; currentMinIndex = j; } if ( currentMinIndex != i ) { list[currentMinIndex] = list[i]; list[i] = currentMin; } -4.5 1.0 4.5 5.7 6.6 9.0

39 Sorting Arrays import java.util.Arrays; double[] grade = {88.6, 98.1, 55.6, 11.2, 99.8}; Arrays.sort(grade); for (int i=0; i<grade.length; i++) System.out.print(grade[i] + " "); Arrays.sort(grade, 1, 3); for (int i=0; i<grade.length; i++) System.out.print(grade[i] + " "); 11.2 55.6 88.6 98.1 99.8 88.6 55.6 98.1 11.2 99.8 INcludingEXcluding

40 Sorting Arrays Other sorting algorithms – insertion sort, – merge sort, – heapsort, – quicksort, – bubble sort, – … 40

41 Searching Sequential search – most efficient if array is presorted Much more efficient – binary search (involves recursion)

42 int[] intList = {12, 45, -6, 0, 55, 65, 53, 45, 22, -54, 33, 5, 12, -62, 45, 45}; System.out.print( "45 found at index " ); for( int i = 0; i < intList.length; i++ ) { if ( intList[ i ] == 45 ) { System.out.print( i + ", " ); matches++; } 42 45 found at index 1, 7, 14, 15,

43 int[] intList = {12, 45, -6, 0, 55, 65, 53, 45, 22, -54, 33, 5, 12, -62, 45, 45}; Arrays.sort( intList ); // import java.util.Arrays; // -62, -54, -6, 0, 5, 12, 12, 22, 33, 45, 45, 45, 45, 53, 55, 65 int matchNumber = 45; System.out.print( matchNumber + " found at index " ); for( int i = 0; i < intList.length; i++ ) { if ( intList[ i ] > matchNumber ) { break; // break loop, you're done // you can do this better, e.g. i = intList.length + 1; } if ( intList[ i ] == matchNumber ) { System.out.print( i + ", " ); matches++; } 43 45 found at index 9, 10, 11, 12,

44 The stranger was working his way along the row. WHAT IS THAT GREEN ONE? The landlord peered at the label. 'It says it's Melon Brandy,' he said doubtfully. 'It says it's bottled by some monks to an ancient recipe,'he added. I WILL TRY IT. The man looked sideways at the empty glasses on the counter, some of them still containing bits of fruit salad, cherries on a stick and small paper umbrellas. 'Are you sure you haven't had enough?' he said. It worried him vaguely that he couldn't seem to make out the stranger's face. The glass, with its drink crystallising out on the sides, disappeared into the hood and came out again empty. NO. WHAT IS THE YELLOW ONE WITH THE WASPS IN IT? 'Spring Cordial, it says. Yes?' YES. AND THEN THE BLUE ONE WITH THE GOLD FLECKS. 'Er. Old Overcoat?' YES. AND THEN THE SECOND ROW. 'Which one did you have in mind?' ALL OF THEM. file: “death tries to get drunk.txt” String book = "death tries to get drunk.txt"; Scanner iInStream = new Scanner( new File( book ) ); String[] word = new String[ 500 ]; int wordCount = 0; while ( iInStream.hasNextLine() ) { String line = iInStream.nextLine(); Scanner iInStreamLine = new Scanner(line); while ( iInStreamLine.hasNext() ) { word[ wordCount ] = iInStreamLine.next(); wordCount++; } int matches = 0; System.out.print( "\"the\" found at index " ); for ( int i = 0; i < wordCount; i++ ) { if ( word[ i ].toLowerCase().equals( "the" ) ) { System.out.println( i + ", " ); matches++; } iInStream.close(); from “Mort” by Terry Pratchett

45 The stranger was working his way along the row. WHAT IS THAT GREEN ONE? The landlord peered at the label. 'It says it's Melon Brandy,' he said doubtfully. 'It says it's bottled by some monks to an ancient recipe,'he added. I WILL TRY IT. The man looked sideways at the empty glasses on the counter, some of them still containing bits of fruit salad, cherries on a stick and small paper umbrellas. 'Are you sure you haven't had enough?' he said. It worried him vaguely that he couldn't seem to make out the stranger's face. The glass, with its drink crystallising out on the sides, disappeared into the hood and came out again empty. NO. WHAT IS THE YELLOW ONE WITH THE WASPS IN IT? 'Spring Cordial, it says. Yes?' YES. AND THEN THE BLUE ONE WITH THE GOLD FLECKS. 'Er. Old Overcoat?' YES. AND THEN THE SECOND ROW. 'Which one did you have in mind?' ALL OF THEM. file: “death tries to get drunk.txt” String book = "death tries to get drunk.txt"; Scanner iInStream = new Scanner( new File( book ) ); String[] word = new String[ 500 ]; int wordCount = 0; while ( iInStream.hasNextLine() ) { String line = iInStream.nextLine(); Scanner iInStreamLine = new Scanner(line); while ( iInStreamLine.hasNext() ) { word[ wordCount ] = iInStreamLine.next(); wordCount++; } int matches = 0; System.out.print( "\"the\" found at index " ); for ( int i = 0; i < wordCount; i++ ) { if ( word[ i ].toLowerCase().equals( "the" ) ) { System.out.println( i + ", " ); matches++; } iInStream.close(); from “Mort” by Terry Pratchett

46 The stranger was working his way along the row. WHAT IS THAT GREEN ONE? The landlord peered at the label. 'It says it's Melon Brandy,' he said doubtfully. 'It says it's bottled by some monks to an ancient recipe,'he added. I WILL TRY IT. The man looked sideways at the empty glasses on the counter, some of them still containing bits of fruit salad, cherries on a stick and small paper umbrellas. 'Are you sure you haven't had enough?' he said. It worried him vaguely that he couldn't seem to make out the stranger's face. The glass, with its drink crystallising out on the sides, disappeared into the hood and came out again empty. NO. WHAT IS THE YELLOW ONE WITH THE WASPS IN IT? 'Spring Cordial, it says. Yes?' YES. AND THEN THE BLUE ONE WITH THE GOLD FLECKS. 'Er. Old Overcoat?' YES. AND THEN THE SECOND ROW. 'Which one did you have in mind?' ALL OF THEM. file: “death tries to get drunk.txt” String book = "death tries to get drunk.txt"; Scanner iInStream = new Scanner( new File( book ) ); String[] word = new String[ 500 ]; int wordCount = 0; while ( iInStream.hasNextLine() ) { String line = iInStream.nextLine(); Scanner iInStreamLine = new Scanner(line); while ( iInStreamLine.hasNext() ) { word[ wordCount ] = iInStreamLine.next(); wordCount++; } int matches = 0; System.out.print( "\"the\" found at index " ); for ( int i = 0; i < wordCount; i++ ) { if ( word[ i ].toLowerCase().equals( "the" ) ) { System.out.println( i + ", " ); matches++; } iInStream.close(); from “Mort” by Terry Pratchett

47 The stranger was working his way along the row. WHAT IS THAT GREEN ONE? The landlord peered at the label. 'It says it's Melon Brandy,' he said doubtfully. 'It says it's bottled by some monks to an ancient recipe,'he added. I WILL TRY IT. The man looked sideways at the empty glasses on the counter, some of them still containing bits of fruit salad, cherries on a stick and small paper umbrellas. 'Are you sure you haven't had enough?' he said. It worried him vaguely that he couldn't seem to make out the stranger's face. The glass, with its drink crystallising out on the sides, disappeared into the hood and came out again empty. NO. WHAT IS THE YELLOW ONE WITH THE WASPS IN IT? 'Spring Cordial, it says. Yes?' YES. AND THEN THE BLUE ONE WITH THE GOLD FLECKS. 'Er. Old Overcoat?' YES. AND THEN THE SECOND ROW. 'Which one did you have in mind?' ALL OF THEM. file: “death tries to get drunk.txt” String book = "death tries to get drunk.txt"; Scanner iInStream = new Scanner( new File( book ) ); String[] word = new String[ 500 ]; int wordCount = 0; while ( iInStream.hasNextLine() ) { String line = iInStream.nextLine(); Scanner iInStreamLine = new Scanner(line); while ( iInStreamLine.hasNext() ) { word[ wordCount ] = iInStreamLine.next(); wordCount++; } int matches = 0; System.out.print( "\"the\" found at index " ); for ( int i = 0; i < wordCount; i++ ) { if ( word[ i ].toLowerCase().equals( "the" ) ) { System.out.println( i + ", " ); matches++; } iInStream.close(); from “Mort” by Terry Pratchett "the" found at index 0, 7, 14, 18, 44, 49, 53, 92, 95, 103, 107, 117, 121, 133, 137, 146,

48 Multi-dimensional arrays …

49 SYNTAX: Base_Type[Row_Index][Column_Index] Array_Name = new Base_Type[Row_Size][Column_Size]; e.g. int[][] budget = new int[10][23]; Person[][] employees = new Person[100][200]; double[][][] someData = double[2][52][7]; …. technically, use as many dimensions as necessary, Java doesn’t constrain that. However, the Java VM limits the dimensions to 255 Multidimensional arrays can be used as variables and method return values/parameters etc. like regular arrays. 49

50 Multi-dimensional arrays 50

51 Multi-dimensional arrays 51 public static final int ROWS = 10; public static final int COLUMNS = 14; public static void main(String[] args) { int[][] multiplicationTable = new int[ ROWS ][ COLUMNS ]; System.out.println( multiplicationTable.length ); System.out.println( multiplicationTable[0].length ); } So, for a 2-dim array double [][] a; a.length  number of rows a[i].length  number of columns 10 14

52 Multi-dimensional arrays 52 public static final int ROWS = 10; public static final int COLUMNS = 10; public static void main(String[] args) { int[][] multiplicationTable = new int[ ROWS ][ COLUMNS ]; for ( int row = 0; row < ROWS; row++ ) for ( int column = 0; column < COLUMNS; column++ ) multiplicationTable[ row ][ column ] = row * column; }

53 53 public static final int ROWS = 10; public static final int COLUMNS = 10; public static void main(String[] args) { int[][] multiplicationTable = new int[ ROWS ][ COLUMNS ]; for ( int row = 0; row < ROWS; row++ ) for ( int column = 0; column < COLUMNS; column++ ) multiplicationTable[ row ][ column ] = row * column; for ( int row = 0; row < ROWS; row++ ) { for ( int column = 0; column < COLUMNS; column++ ) { System.out.printf( "%d \t", multiplicationTable[ row ][ column ] ); } System.out.println(); } 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 0 2 4 6 8 10 12 14 16 18 0 3 6 9 12 15 18 21 24 27 0 4 8 12 16 20 24 28 32 36 0 5 10 15 20 25 30 35 40 45 0 6 12 18 24 30 36 42 48 54 0 7 14 21 28 35 42 49 56 63 0 8 16 24 32 40 48 56 64 72 0 9 18 27 36 45 54 63 72 81 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 0 2 4 6 8 10 12 14 16 18 0 3 6 9 12 15 18 21 24 27 0 4 8 12 16 20 24 28 32 36 0 5 10 15 20 25 30 35 40 45 0 6 12 18 24 30 36 42 48 54 0 7 14 21 28 35 42 49 56 63 0 8 16 24 32 40 48 56 64 72 0 9 18 27 36 45 54 63 72 81

54 54 public static final int ROWS = 10; public static final int COLUMNS = 10; public static void main(String[] args) { int[][] multiplicationTable = new int[ ROWS ][ COLUMNS ]; for ( int row = 0; row < ROWS; row++ ) for ( int column = 0; column < COLUMNS; column++ ) multiplicationTable[ row ][ column ] = row * column; for ( int row = 0; row < ROWS; row++ ) { System.out.printf( "row %d: \t", row ); for ( int column = 0; column < COLUMNS; column++ ) { System.out.printf( "%d \t", multiplicationTable[ row ][ column ] ); } System.out.println(); } row 0: 0 0 0 0 0 0 0 0 0 0 row 1: 0 1 2 3 4 5 6 7 8 9 row 2: 0 2 4 6 8 10 12 14 16 18 row 3: 0 3 6 9 12 15 18 21 24 27 row 4: 0 4 8 12 16 20 24 28 32 36 row 5: 0 5 10 15 20 25 30 35 40 45 row 6: 0 6 12 18 24 30 36 42 48 54 row 7: 0 7 14 21 28 35 42 49 56 63 row 8: 0 8 16 24 32 40 48 56 64 72 row 9: 0 9 18 27 36 45 54 63 72 81 row 0: 0 0 0 0 0 0 0 0 0 0 row 1: 0 1 2 3 4 5 6 7 8 9 row 2: 0 2 4 6 8 10 12 14 16 18 row 3: 0 3 6 9 12 15 18 21 24 27 row 4: 0 4 8 12 16 20 24 28 32 36 row 5: 0 5 10 15 20 25 30 35 40 45 row 6: 0 6 12 18 24 30 36 42 48 54 row 7: 0 7 14 21 28 35 42 49 56 63 row 8: 0 8 16 24 32 40 48 56 64 72 row 9: 0 9 18 27 36 45 54 63 72 81

55 55 public static final int ROWS = 10; public static final int COLUMNS = 10; public static void main(String[] args) { int[][] multiplicationTable = new int[ ROWS ][ COLUMNS ]; for ( int row = 0; row < ROWS; row++ ) for ( int column = 0; column < COLUMNS; column++ ) multiplicationTable[ row ][ column ] = row * column; System.out.print( "r/c: \t" ); for ( int column = 0; column < COLUMNS; column++ ) System.out.printf( "col %d: \t", column ); System.out.println(); for ( int row = 0; row < ROWS; row++ ) { System.out.printf( "row %d: \t", row ); for ( int column = 0; column < COLUMNS; column++ ) { System.out.printf( "%d \t", multiplicationTable[ row ][ column ] ); } System.out.println(); } r/c: col 0: col 1: col 2: col 3: col 4: col 5: col 6: col 7: col 8: col 9: row 0: 0 0 0 0 0 0 0 0 0 0 row 1: 0 1 2 3 4 5 6 7 8 9 row 2: 0 2 4 6 8 10 12 14 16 18 row 3: 0 3 6 9 12 15 18 21 24 27 row 4: 0 4 8 12 16 20 24 28 32 36 row 5: 0 5 10 15 20 25 30 35 40 45 row 6: 0 6 12 18 24 30 36 42 48 54 row 7: 0 7 14 21 28 35 42 49 56 63 row 8: 0 8 16 24 32 40 48 56 64 72 row 9: 0 9 18 27 36 45 54 63 72 81 r/c: col 0: col 1: col 2: col 3: col 4: col 5: col 6: col 7: col 8: col 9: row 0: 0 0 0 0 0 0 0 0 0 0 row 1: 0 1 2 3 4 5 6 7 8 9 row 2: 0 2 4 6 8 10 12 14 16 18 row 3: 0 3 6 9 12 15 18 21 24 27 row 4: 0 4 8 12 16 20 24 28 32 36 row 5: 0 5 10 15 20 25 30 35 40 45 row 6: 0 6 12 18 24 30 36 42 48 54 row 7: 0 7 14 21 28 35 42 49 56 63 row 8: 0 8 16 24 32 40 48 56 64 72 row 9: 0 9 18 27 36 45 54 63 72 81

56

57 What is the type of ? A.int B.boolean C.double D.double[] E.none 57 double[] grade = {88.6, 98.1, 55.6, 11.2, 99.8}; double[] oGrade = new double[5]; double m = 0.0; int index = 0; for ( int i = 0; i < grade.length; i++ ) { m = 0.0; for ( int j = 0; j < grade.length; j++ ) { if ( m < grade[ j ] ) { m = grade[ j ]; index = j; } oGrade[ i ] = grade[ index ]; grade[ index ] = 0; }

58 What is the type of ? A.int B.boolean C.double D.double[] E.none 58 double[] grade = {88.6, 98.1, 55.6, 11.2, 99.8}; double[] oGrade = new double[5]; double m = 0.0; int index = 0; for ( int i = 0; i < grade.length; i++ ) { m = 0.0; for ( int j = 0; j < grade.length; j++ ) { if ( m < grade[ j ] ) { m = grade[ j ]; index = j; } oGrade[ i ] = grade[ index ]; grade[ index ] = 0; }

59 What is the type of ? A.int B.boolean C.double D.double[] E.none 59 double[] grade = {88.6, 98.1, 55.6, 11.2, 99.8}; double[] oGrade = new double[5]; double m = 0.0; int index = 0; for ( int i = 0; i < grade.length; i++ ) { m = 0.0; for ( int j = 0; j < grade.length; j++ ) { if ( m < grade[ j ] ) { m = grade[ j ]; index = j; } oGrade[ i ] = grade[ index ]; grade[ index ] = 0; }

60 What is the type of ? A.int B.boolean C.double D.double[] E.none 60 double[] grade = {88.6, 98.1, 55.6, 11.2, 99.8}; double[] oGrade = new double[5]; double m = 0.0; int index = 0; for ( int i = 0; i < grade.length; i++ ) { m = 0.0; for ( int j = 0; j < grade.length; j++ ) { if ( m < grade[ j ] ) { m = grade[ j ]; index = j; } oGrade[ i ] = grade[ index ]; grade[ index ] = 0; }

61 What is the type of ? A.int B.boolean C.double D.double[] E.none 61 double[] grade = {88.6, 98.1, 55.6, 11.2, 99.8}; double[] oGrade = new double[5]; double m = 0.0; int index = 0; for ( int i = 0; i < grade.length; i++ ) { m = 0.0; for ( int j = 0; j < grade.length; j++ ) { if ( m < grade[ j ] ) { m = grade[ j ]; index = j; } oGrade[ i ] = grade[ index ]; grade[ index ] = 0; }

62 What is the type of ? A.int B.boolean C.double D.double[] E.none 62 double[] grade = {88.6, 98.1, 55.6, 11.2, 99.8}; double[] oGrade = new double[5]; double m = 0.0; int index = 0; for ( int i = 0; i < grade.length; i++ ) { m = 0.0; for ( int j = 0; j < grade.length; j++ ) { if ( m < grade[ j ] ) { m = grade[ j ]; index = j; } oGrade[ i ] = grade[ index ]; grade[ index ] = 0; } oGrade[ i ] = 90.8;

63 What is the type of ? A.String B.boolean C.double[] D.char[] E.char 63 public static doubleString( String s ) { s = s + s; char[] result = s.toUpperCase().toCharArray(); return result; }

64 What is the type of ? A.int B.boolean C.int[] D.String E.char[] 64 public static void selectionSort( list ) { for ( int i = 0; i < list.length - 1; i++ ) { int currentMin = list[ i ]; int currentMinIndex = i; for ( int j = i + 1; j < list.length; j++ ) { if (currentMin > list[ j ]) { currentMin = list[ j ]; currentMinIndex = j; } if ( currentMinIndex != i ) { list[ currentMinIndex ] = list[ i ]; list[ i ] = currentMin; }

65 What is the type of ? A.int B.boolean C.Travel[] D.String E.Trip 65 public class Trip { private String countryName; private String city; private String dateOfTrip; public String traveller; …… } public class Travel{ public static void main(String[] args) { Trip t_1 = new Trip("Germany", "Hamburg", "12-June-2010"); Trip t_2 = new Trip("Holland", "Amsterdam", "18-July-2011"); [] vacations = new [10]; vacations[0] = t_1; vacations[0].traveller = "Me"; }

66 What is the type of ? A.int B.boolean C.Trip[] D.String E.Trip 66 public class Trip { private String countryName; private String city; private String dateOfTrip; public String traveller; …… } public class Travel { public static void main(String[] args) { Trip t_1 = new Trip("Germany", "Hamburg", "12-June-2010"); Trip t_2 = new Trip("Holland", "Amsterdam", "18-July-2011"); Trip[] vacations = new Trip[10]; vacations[0] = t_1; vacations[0].traveller = "Me"; }

67 What is the type of ? A.int B.boolean C.Trip[] D.String E.Trip 67 public class Trip { private String countryName; private String city; private String dateOfTrip; public String traveller; …… } public class Travel { public static void main(String[] args) { Trip t_1 = new Trip("Germany", "Hamburg", "12-June-2010"); Trip t_2 = new Trip("Holland", "Amsterdam", "18-July-2011"); Trip[] vacations = new Trip[10]; vacations[0] = t_1; vacations[0].traveller = "Me"; }

68 What is the type of ? A.int B.boolean C.Trip[] D.String E.Trip 68 public class Trip { private String countryName; private String city; private String dateOfTrip; public String traveller; …… } public class Travel { public static void main(String[] args) { Trip t_1 = new Trip("Germany", "Hamburg", "12-June-2010"); Trip t_2 = new Trip("Holland", "Amsterdam", "18-July-2011"); Trip[] vacations = new Trip[10]; vacations[0] = t_1; vacations[0].traveller = "Me"; }

69 What is the type of ? A.int B.boolean C.Trip[] D.String E.Trip 69 public class Trip { private String countryName; private String city; private String dateOfTrip; public String traveller; …… } public class Travel { public static void main(String[] args) { Trip t_1 = new Trip("Germany", "Hamburg", "12-June-2010"); Trip t_2 = new Trip("Holland", "Amsterdam", "18-July-2011"); Trip[] vacations = new Trip[10]; vacations[0] = t_1; vacations[0].traveller = "Me"; }

70 What is the type of ? A.int B.boolean C.Trip[] D.String E.Trip 70 public class Trip { private String countryName; private String city; private String dateOfTrip; public String traveller; … public getTrip(){ return this.countryName + this.dateOfTrip + " - " + this.traveller; } public class Travel { public static void main(String[] args) { Trip t_1 = new Trip("Germany", "Hamburg", "12-June-2010"); Trip t_2 = new Trip("Holland", "Amsterdam", "18-July-2011"); Trip[] vacations = new Trip[10]; vacations[0] = t_1; vacations[0].traveller = "Me"; }

71 What is the type of ? A.int B.boolean C.String[] D.int[] E.String 71 String book = "death tries to get drunk.txt"; Scanner iInStream = new Scanner( new File( book ) ); String[] word = new String[ 500 ]; int wordCount = 0; while ( iInStream.hasNextLine() ) { String line = iInStream.nextLine(); Scanner iInStreamLine = new Scanner(line); while ( iInStreamLine.hasNext() ) { word[ wordCount ] = iInStreamLine.next(); wordCount++; } int matches = 0; System.out.print( "\"the\" found at index " ); for ( int i = 0; i < wordCount; i++ ) { if ( word[ i ].toLowerCase().equals( "the" ) ) { System.out.println( i + ", " ); matches++; } iInStream.close();

72 What is the type of ? A.int B.boolean C.String[] D.int[] E.String 72 String book = "death tries to get drunk.txt"; Scanner iInStream = new Scanner( new File( book ) ); String[] word = new String[ 500 ]; int wordCount = 0; while ( iInStream.hasNextLine() ) { String line = iInStream.nextLine(); Scanner iInStreamLine = new Scanner(line); while ( iInStreamLine.hasNext() ) { word[ wordCount ] = iInStreamLine.next(); wordCount++; } int matches = 0; System.out.print( "\"the\" found at index " ); for ( int i = 0; i < wordCount; i++ ) { if ( word[ i ].toLowerCase().equals( "the" ) ) { System.out.println( i + ", " ); matches++; } iInStream.close();

73 What is the type of ? A.int B.boolean C.String[] D.int[] E.String 73 String book = "death tries to get drunk.txt"; Scanner iInStream = new Scanner( new File( book ) ); String[] word = new String[ 500 ]; int wordCount = 0; while ( iInStream.hasNextLine() ) { String line = iInStream.nextLine(); Scanner iInStreamLine = new Scanner(line); while ( iInStreamLine.hasNext() ) { word[ wordCount ] = iInStreamLine.next(); wordCount++; } int matches = 0; System.out.print( "\"the\" found at index " ); for ( int i = 0; i < wordCount; i++ ) { if ( word[ i ].toLowerCase().equals( "the" ) ) { System.out.println( i + ", " ); matches++; } iInStream.close();

74 What is the type of ? A.int B.boolean C.String[] D.int[] E.String 74 String book = "death tries to get drunk.txt"; Scanner iInStream = new Scanner( new File( book ) ); String[] word = new String[ 500 ]; int wordCount = 0; while ( iInStream.hasNextLine() ) { String line = iInStream.nextLine(); Scanner iInStreamLine = new Scanner(line); while ( iInStreamLine.hasNext() ) { word[ wordCount ] = iInStreamLine.next(); wordCount++; } int matches = 0; System.out.print( "\"the\" found at index " ); for ( int i = 0; i < wordCount; i++ ) { if ( word[ i ].toLowerCase().equals( "the" ) ) { System.out.println( i + ", " ); matches++; } iInStream.close();

75 What is the type of ? A.int B.boolean C.String[] D.int[] E.String 75 String book = "death tries to get drunk.txt"; Scanner iInStream = new Scanner( new File( book ) ); String[] word = new String[ 500 ]; int wordCount = 0; while ( iInStream.hasNextLine() ) { String line = iInStream.nextLine(); Scanner iInStreamLine = new Scanner(line); while ( iInStreamLine.hasNext() ) { word[ wordCount ] = iInStreamLine.next(); wordCount++; } int matches = 0; System.out.print( "\"the\" found at index " ); for ( int i = 0; i < wordCount; i++ ) { if ( word[ i ].toLowerCase().equals( "the" ) ) { System.out.println( i + ", " ); matches++; } iInStream.close();

76 What is the type of ? A.int B.boolean C.String[] D.int[] E.String 76 String book = "death tries to get drunk.txt"; Scanner iInStream = new Scanner( new File( book ) ); String[] word = new String[ 500 ]; int wordCount = 0; while ( iInStream.hasNextLine() ) { String line = iInStream.nextLine(); Scanner iInStreamLine = new Scanner(line); while ( iInStreamLine.hasNext() ) { word[ wordCount ] = iInStreamLine.next(); wordCount++; } int matches = 0; System.out.print( "\"the\" found at index " ); for ( int i = 0; i < wordCount; i++ ) { if ( word[ i ].toLowerCase().equals( "the" ) ) { System.out.println( i + ", " ); matches++; } iInStream.close();

77 What is the type of ? A.int B.boolean C.String[] D.int[] E.String 77 String book = "death tries to get drunk.txt"; Scanner iInStream = new Scanner( new File( book ) ); String[] word = new String[ 500 ]; int wordCount = 0; while ( iInStream.hasNextLine() ) { String line = iInStream.nextLine(); Scanner iInStreamLine = new Scanner(line); while ( iInStreamLine.hasNext() ) { word[ wordCount ] = iInStreamLine.next(); wordCount++; } int matches = 0; System.out.print( "\"the\" found at index " ); for ( int i = 0; i < wordCount; i++ ) { if ( word[ i ].toLowerCase().equals( "the" ) ) { System.out.println( i + ", " ); matches++; } iInStream.close();

78 What is the type of ? A.int[] B.boolean[] C.int[][] D.int E.String 78 int[][] multiplicationTable = new int[ROWS][COLUMNS]; for ( int row = 0; row < ROWS; row++ ) { for ( int column = 0; column < COLUMNS; column++ ) { multiplicationTable[row][column] = row*column; }

79 What is the type of ? A.int[] B.boolean[] C.int[][] D.int E.String 79 int[][] multiplicationTable = new int[ROWS][COLUMNS]; for ( int row = 0; row < ROWS; row++ ) { for ( int column = 0; column < COLUMNS; column++ ) { multiplicationTable[row][column] = row*column; }

80 What is the type of ? A.int[] B.boolean[] C.int[][] D.int E.String 80 int[][] multiplicationTable = new int[ROWS][COLUMNS]; for ( int row = 0; row < ROWS; row++ ) { for ( int column = 0; column < COLUMNS; column++ ) { multiplicationTable[row][column] = row*column; }


Download ppt "Arrays CS140: Introduction to Computing 1 Savitch Chapter 7 10/23/13 10/28/13."

Similar presentations


Ads by Google