Skip to content
Ameen Abozaid
BEG-015

Arrays and strings

  • 8 Lessons
  • 84 min of reading
  • 5 h practice (estimate)
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

  1. Create, fill, iterate, and copy one- and two-dimensional arrays.
  2. Use common String methods (length, charAt, substring, indexOf, split, equals, trim, toUpperCase).
  3. Explain string immutability and use StringBuilder for repeated concatenation.
  4. Avoid ArrayIndexOutOfBoundsException by reasoning about valid indices.

Lessons

  1. 01Arrays: fixed-size lists10 min read
  2. 02Iterating and enhanced for11 min read
  3. 03Two-dimensional arrays10 min read
  4. 04Working with Strings10 min read
  5. 05Immutability and StringBuilder11 min read
  6. 06Arrays utility class10 min read
  7. 07Demo: student-grade manager with arrays11 min read
  8. 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

  1. Store student names and grades in arrays (read them or start with sample data).
  2. Compute the average, highest, and lowest grade, and a letter grade for each student.
  3. Search a student by name (ignore case) and show the grade, or "not found".
  4. Print an aligned report.
  5. Handle an empty class without crashing.

Acceptance: correct statistics; case-insensitive search; handles empty class.

Exercises (assessment: seven exercises plus the grade manager)

IDOutcomeTask
BEG-015-EX011Create an int[] of 5 numbers, fill it with a loop, print it with Arrays.toString, and make a real copy.
BEG-015-EX021Create a 3x3 int[][] and print its diagonal and the sum of all cells.
BEG-015-EX032Given " Hello, World ": trim it, get its length, the index of "World", and the substring "Hello".
BEG-015-EX042Split "ana,bob,carl" and print each name in upper case.
BEG-015-EX053Explain why s.toUpperCase(); alone does not change s.
BEG-015-EX063Build the string "1,2,3,...,1000" with StringBuilder and explain why not +=.
BEG-015-EX074List 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 = a would not be a copy).
  • EX02 Diagonal g[i][i]; sum with two nested loops over g.length and g[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.

  1. 1The first index of an array is:O1
  2. 2new int[5] creates an array whose elements start as:O1
  3. 3grid[2][3] in a 2D array means:O1
  4. 4To compare two Strings' text, use:O2
  5. 5"hello".substring(1, 3) is:O2
  6. 6indexOf returns what when the text is missing?O2
  7. 7Strings are immutable. So s.toUpperCase(); alone:O3
  8. 8For repeated concatenation in a loop, use:O3
  9. 9For an array of length 5, the last valid index is:O4
  10. 10arr[arr.length] causes:O4

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).

Type to search levels, phases, modules and lessons.