Chapter 9: Working with Arrays and Strings πΒΆ
Prerequisites: Basic Java knowledge
This cheat sheet covers arrays and string handling for Chapter 9 of the GSEB Std 12 Computer Studies syllabus.
π― Key ConceptsΒΆ
- Array: A fixed-size collection of data of the same type stored in continuous memory.
- String: An immutable object representing a sequence of characters.
- Index: Position of an element (starts from 0).
π οΈ Java Arrays (1D & 2D)ΒΆ
1. 1D Array SyntaxΒΆ
- Declaration:
int[] marks; - Creation:
marks = new int[5];(Values initialized to 0 by default). - Access:
marks[0] = 95;
2. 2D Array Syntax (Matrix)ΒΆ
- Declaration:
int[][] matrix = new int[2][3];(2 rows, 3 columns). - Access:
matrix[row][col].
3. The java.util.Arrays Helper ClassΒΆ
| Method | Description |
|---|---|
Arrays.sort(arr) |
Sorts the array in ascending order. |
Arrays.fill(arr, val) |
Fills the whole array with a single value. |
Arrays.equals(arr1, arr2) |
Returns true if two arrays have same elements in same order. |
Arrays.binarySearch(arr, key) |
Finds the index of an element (array must be sorted). |
π Java Strings (Immutable)ΒΆ
1. Key PropertiesΒΆ
- Immutability: Once a String is created, its value cannot be changed.
- String Pool: Efficient memory storage for literals.
2. Essential String MethodsΒΆ
| Method | Type | Description |
|---|---|---|
length() |
Method | Returns the number of characters. |
charAt(i) |
Method | Returns character at index i. |
substring(start, end) |
Method | Returns part of string from start to end-1. |
equals(other) |
Method | Case-sensitive equality check. |
equalsIgnoreCase(other) |
Method | Case-insensitive equality check. |
replace('o', 'a') |
Method | Replaces all 'o' with 'a'. |
π‘ Board Focus: High-Weightage Points πΒΆ
- MCQ Alert:
lengthis a property for arrays (arr.length);length()is a method for strings (s.length()). - MCQ Alert: Index of the last element is always
length - 1. - MCQ Alert: Array size cannot be changed after creation.
- MCQ Alert:
CalendarandDateclasses are in thejava.utilpackage. - MCQ Alert:
System.arraycopy()is used to copy arrays.
Board Exam Secret
GSEB often asks about the "Default Values" of arrays. Remember: int = 0, boolean = false, float/double = 0.0, and objects = null.
Std 12 Hub | [End of Series]