Download presentation
Presentation is loading. Please wait.
Published byDarlene White Modified over 9 years ago
1
Arrays and Strings
2
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.
3
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];
4
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 6 th element in hw1Scores hw1Scores[5] 89 78 2377 … 0 567897
5
Use and modify an array element hw1Scores[5] = 70; hw1Scores[5] = hw1Scores[5] + 2; System.out.print(hw1Scores[5]);
6
Adding all the scores to find average
7
int sum = hw1Scores[0]; for (int i = 1; i < 30; i++) { sum = sum + hw1Scores[i]; } double average = sum / 30.0;
8
Adding all the scores to find average int sum = hw1Scores[0]; for (int i = 1; i < hw1Scores.length; i++) { sum = sum + hw1Scores[i]; } double average = sum / (double)hw1Scores.length;
9
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];
10
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 … ?
11
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, ?th hw ?
12
Average of the hw2 scores? What are the indexes? Scores[?][?] 0 1 2 3 4 5 6 7 …. 0 1 2 3 4 5 …
13
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;
14
Average score for the 4 th student? What are the indexes? Scores[?][?] 0 1 2 3 4 5 6 7 …. 0 1 2 3 4 5 …
15
Average of the 4 th 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;
16
Average of the 4 th 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
17
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”;
18
String Operaters + Concatenation—combining two strings String name = firstName + ” ” + lastName;
19
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.