Arrays and strings
On this page 8 topics
Overview
Topics
- One-dimensional/multidimensional arrays
- String
- StringBuilder
- common operations
Learn first
84 min of reading · 5 h of practice (estimate)
Learning outcomes
- Create, fill, iterate, and copy one- and two-dimensional arrays.
- Use common
Stringmethods (length, charAt, substring, indexOf, split, equals, trim, toUpperCase). - Explain string immutability and use
StringBuilderfor repeated concatenation. - Avoid
ArrayIndexOutOfBoundsExceptionby reasoning about valid indices.
Lessons
- 01Arrays: fixed-size lists10 min read
- 02Iterating and enhanced for11 min read
- 03Two-dimensional arrays10 min read
- 04Working with Strings10 min read
- 05Immutability and StringBuilder11 min read
- 06Arrays utility class10 min read
- 07Demo: student-grade manager with arrays11 min read
- 08Demo: string bugs: ==, off-by-one, and slow concatenation11 min read
Lab and exercises
Lab
BEG-015-LAB1 · Student-grade manager (mini-project 3 of 3)
Outcomes: 1–4 · Time: ~3 h
- Store student names and grades in arrays (read them or start with sample data).
- Compute the average, highest, and lowest grade, and a letter grade for each student.
- Search a student by name (ignore case) and show the grade, or "not found".
- Print an aligned report.
- Handle an empty class without crashing.
Acceptance: correct statistics; case-insensitive search; handles empty class.
Exercises (assessment: seven exercises plus the grade manager)
| ID | Outcome | Task |
|---|---|---|
| BEG-015-EX01 | 1 | Create an int[] of 5 numbers, fill it with a loop, print it with Arrays.toString, and make a real copy. |
| BEG-015-EX02 | 1 | Create a 3x3 int[][] and print its diagonal and the sum of all cells. |
| BEG-015-EX03 | 2 | Given " Hello, World ": trim it, get its length, the index of "World", and the substring "Hello". |
| BEG-015-EX04 | 2 | Split "ana,bob,carl" and print each name in upper case. |
| BEG-015-EX05 | 3 | Explain why s.toUpperCase(); alone does not change s. |
| BEG-015-EX06 | 3 | Build the string "1,2,3,...,1000" with StringBuilder and explain why not +=. |
| BEG-015-EX07 | 4 | List the valid indices for new int[4]; fix a loop with i <= arr.length. |
Show solutions
- EX01
int[] a = new int[5]; for (int i = 0; i < a.length; i++) a[i] = i * 2; int[] b = Arrays.copyOf(a, a.length);(b = awould not be a copy). - EX02 Diagonal
g[i][i]; sum with two nested loops overg.lengthandg[r].length. - EX03
t = s.trim()gives "Hello, World"; length 12;t.indexOf("World")is 7;t.substring(0, 5)is "Hello". - EX04
for (String n : "ana,bob,carl".split(",")) System.out.println(n.toUpperCase()); - EX05 Strings are immutable; the method returns a new String that must be assigned:
s = s.toUpperCase();. - EX06
StringBuilder sb; for (i = 1..1000) { if (i > 1) sb.append(','); sb.append(i); }—+=copies the whole string each time. - EX07 Valid indices are 0, 1, 2, 3. Change
<=to<. - Outcome 4 is also tested in the lab (empty class) and quiz.
Quiz
Outcomes: O1 arrays · O2 String methods · O3 immutability and StringBuilder · O4 valid indices.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Seven exercises BEG-015-EX01 to EX07 plus the student-grade manager. Pass: at least six exercises correct and a grade manager with correct statistics, case-insensitive search, and empty-class handling.
Common mistakes
Comparing strings with ==; off-by-one on length; concatenating in loops.
Assessment
7 exercises plus the grade manager.
Career relevance
Arrays underpin collections (BEG-030) and data structures (BEG-040).