Lesson Completion
Back to course

One-Dimensional Arrays: Declaration, Creation, and Access

Beginner
15 minutesā˜…4.5Java

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

graph LR subgraph "int[] numbers = new int[5]" I0["[0]=10"] --> I1["[1]=20"] I1 --> I2["[2]=30"] I2 --> I3["[3]=40"] I3 --> I4["[4]=50"] end style I0 fill:#2E7D32 style I2 fill:#F57C00

3. Technical Mastery (The "Deep Dive")

šŸ“˜ Three Steps

StepSyntaxPurpose
Declarationint[] arr;Define type and name
Creationarr = new int[5];Allocate memory
Combinedint[] arr = new int[5];Both in one line

Default Values

TypeDefault
int, long, short, byte0
double, float0.0
booleanfalse
char'\u0000'
Objectsnull

4. Interactive & Applied Code

java
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

java
int[] arr = new int[5]; arr[5] = 10; // āŒ Valid: 0-4, not 5!

Mistake #2: Uninitialized array access

java
int[] arr; arr[0] = 10; // āŒ NullPointerException! Not created yet

Mistake #3: Negative index

java
arr[-1] = 10; // āŒ ArrayIndexOutOfBoundsException

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

Topics Covered

Java FundamentalsArrays

Tags

#java#arrays#data-structures#multidimensional-arrays#beginner-friendly

Last Updated

2025-02-01