1. The Hook (The "Byte-Sized" Intro)
In a Nutshell: One-dimensional arrays are linear collections where elements are stored sequentially. Declaration defines the type, creation allocates memory, and access retrieves/modifies elements by index.
When Spotify stores your playlist: Song[] playlist = new Song[100]. Declare type, create space, access by position!
2. Conceptual Clarity (The "Simple" Tier)
š” The Analogy: Train Cars
Think of a 1D array as a train:
- Declaration = Deciding train type (passenger, freight)
- Creation = Building the train with N cars
- Access = Boarding car #5 directly
Each car holds one item, numbered from 0!
Visual Map
3. Technical Mastery (The "Deep Dive")
š Three Steps
| Step | Syntax | Purpose |
|---|---|---|
| Declaration | int[] arr; | Define type and name |
| Creation | arr = new int[5]; | Allocate memory |
| Combined | int[] arr = new int[5]; | Both in one line |
Default Values
| Type | Default |
|---|---|
int, long, short, byte | 0 |
double, float | 0.0 |
boolean | false |
char | '\u0000' |
| Objects | null |
4. Interactive & Applied Code
public class OneDArrayDemo {
public static void main(String[] args) {
// Declaration only
int[] numbers;
// Creation (instantiation)
numbers = new int[5];
// Combined declaration + creation
String[] names = new String[3];
// Assigning values
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Accessing values
System.out.println("First: " + numbers[0]); // 10
System.out.println("Last: " + numbers[4]); // 50
System.out.println("Length: " + numbers.length); // 5
// Static initialization (shorthand)
int[] primes = {2, 3, 5, 7, 11};
String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri"};
// Anonymous array (inline)
printArray(new int[]{100, 200, 300});
// Modify elements
numbers[2] = 999;
System.out.println("Modified: " + numbers[2]); // 999
// Last element access pattern
int lastIdx = numbers.length - 1;
System.out.println("Last: " + numbers[lastIdx]);
}
static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}ā ļø Common Mistakes
Mistake #1: Index out of bounds
int[] arr = new int[5];
arr[5] = 10; // ā Valid: 0-4, not 5!Mistake #2: Uninitialized array access
int[] arr;
arr[0] = 10; // ā NullPointerException! Not created yetMistake #3: Negative index
arr[-1] = 10; // ā ArrayIndexOutOfBoundsException5. The "Interview Corner"
š Q1: "Difference between int arr[] and int[] arr?"
Answer: Same thing. int[] arr is preferred (type and brackets together). int arr[] is C-style, allowed for compatibility.
š Q2: "What's the default value of new int[5]?"
Answer: All elements are 0. Java initializes numeric arrays to 0, booleans to false, objects to null.
š Q3: "Can array size be negative?" Answer: NegativeArraySizeException thrown at runtime.
š Key Takeaways
ā
Declaration + Creation = Two steps (can combine)
ā
Access/modify using arr[index]
ā
Valid indices: 0 to length-1
ā
Default values assigned automatically
ā
Static init: {1, 2, 3} for known values