200 likes | 335 Views
Arrays and Strings. Why?. Consider a class of 30 students, each has a score for hw1 Do we want to have 30 variables with different names? Would it be easier if we have only one name for all 30 scores? We can create structures that are composed of multiple items. Arrays.
E N D
Why? • Consider a class of 30 students, each has a score for hw1 • Do we want to have 30 variables with different names? • Would it be easier if we have only one name for all 30 scores? • We can create structures that are composed of multiple items.
Arrays • A structure that allows multiple items of the same type • Declaring an array of 30 integers • Allocating space in the memory int[] hw1Scores = new int[30]; • Declaring an array of 30 doubles double[] gpa= new double[30]; • Generally type[] name = new type[size];
Accessing an element in an array • Each element is identified by an “index” • First element has an index of 0 • Last element has an index of (size – 1), eg. 29 in hw1Scores 0 1 2 3 4 5 … 28 29 • 6th element in hw1Scores • hw1Scores[5] 89 23 77 56 78 97 78 … 0
Use and modify an array element hw1Scores[5] = 70; hw1Scores[5] = hw1Scores[5] + 2; System.out.print(hw1Scores[5]);
Adding all the scores to find average int sum = hw1Scores[0]; for (inti = 1; i < 30; i++) { sum = sum + hw1Scores[i]; } double average = sum / 30.0;
Adding all the scores to find average int sum = hw1Scores[0]; for (inti = 1; i < hw1Scores.length; i++) { sum = sum + hw1Scores[i]; } double average = sum / (double)hw1Scores.length;
2-Dimensional Arrays • Similar to a table (matrix), with rows and columns • 30 students (rows), 10 scores (columns) • Declaring 30 x 10 integer array int[][] scores = new int[30][10]; • Generally type[][] name = new type[rows][columns];
Accessing 2D Array Elements • Similar to 1D arrays • Index starts at 0 0 1 2 3 4 5 6 7 …. 0 1 2 3 4 5 …
Accessing 2D Array Elements • Similar to 1D arrays • Index starts at 0 0 1 2 3 4 5 6 7 …. 0 1 2 3 4 5 … scores[4][5] -- ?th student , ?thhw
Average of the hw2 scores? • What are the indexes? • Scores[?][?] 0 1 2 3 4 5 6 7 …. 0 1 2 3 4 5 …
hw2 average int sum = scores[0][1]; for (int row = 1; row < scores.length; row++) { sum = sum + scores[row][1]; } double average = sum / (double)scores.length;
Average score for the 4th student? • What are the indexes? • Scores[?][?] 0 1 2 3 4 5 6 7 …. 0 1 2 3 4 5 …
Average of the 4th student int sum = scores[3][0]; for (int col = 1; col < scores[3].length; col++) { sum = sum + scores[3][col]; } double average = sum / (double)scores[3].length;
Average of the 4th student int sum = scores[3][0]; for (int col = 1; col < scores[0].length; col++) { sum = sum + scores[3][col]; } double average = sum / (double)scores[0].length; Same number of columns in all rows
Strings • Multiple characters • Delimited by double quotes • ”Hello, my name is Mickey Mouse.” • Declaration (allocating space in memory) • String studentName; // note capital S • Initialization • String studentName = ”John Smith”;
String Operaters • + • Concatenation—combining two strings • String name = firstName + ” ” + lastName;
String Operations/Methods • equals() • yields boolean (true/false) • generally more appropriate than == • studentName.equals(personName) • equalsIgnoreCase() • Not case sensitive • String name1 = ”John”, name2 = ”john”; • name1.equalsIgnoreCase(name2) // yields true • charAt(i) • yields a character at index i • name1.charAt(3) // yields n • indexOf(str) • yields the index of where str starts • name1.indexOf(”oh”) // yields 1