0% found this document useful (0 votes)
4 views4 pages

Java Arrays: Declaration & Usage Guide

This document provides an overview of arrays in Java, detailing their declaration, initialization, indexing, traversal, and how to pass them by reference. It explains the characteristics of arrays, including their fixed size, zero-based indexing, and memory allocation. Additionally, it covers common errors like ArrayIndexOutOfBoundsException and performance considerations related to memory and access time.

Uploaded by

marcelinomiks
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Java Arrays: Declaration & Usage Guide

This document provides an overview of arrays in Java, detailing their declaration, initialization, indexing, traversal, and how to pass them by reference. It explains the characteristics of arrays, including their fixed size, zero-based indexing, and memory allocation. Additionally, it covers common errors like ArrayIndexOutOfBoundsException and performance considerations related to memory and access time.

Uploaded by

marcelinomiks
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

📚 Arrays in Java Essentials

Brief Overview
This note covering Java arrays was created from the article "Arrays in Java" from
GeeksforGeeks.
It covers array declaration, initialization, indexing, traversal, and passing arrays by reference.

Key Points
Declaring and initializing arrays
Accessing and updating elements
Traversing arrays and working with object arrays

📚 Overview of Java Arrays


Array – a linear data structure in Java that stores multiple values of the same type.
Arrays are objects; they inherit from [Link], so methods like toString(), equals(), and
hashCode() are available.

Contiguous memory for primitive elements; for reference types, contiguous locations
hold the references.
Zero‑based indexing – first element is at index 0.
Fixed length – size is set when the array is created and cannot change later.
Built‑in length property provides the number of elements.

🛠️ Declaring and Initializing Arrays


Declaration
The declaration specifies the element type and the variable name; it does not create an actual
array.

int[] numbers; // declares a variable that can hold an int array


Student[] roster; // declares a variable for an array of Student objects

Initialization with new


Allocates memory on the heap and creates a reference.
Elements are automatically initialized to:
0 for numeric types
false for boolean
null for reference types

int[] numbers = new int[10]; // array of 10 ints, all initialized to 0


Student[] roster = new Student[5]; // array of 5 Student references, all null

Array Literals
Use when the size and values are known at compile time.
In recent Java versions, the new int[] part can be omitted.

int[] arr = {1,2,3,4,5,6,7,8,9,10};

🔢 Accessing and Updating Elements


Indexing Rules
Valid indices: 0 to [Link] - 1.
Accessing an illegal index throws ArrayIndexOutOfBoundsException.
Changing an Element

arr[0] = 90; // sets the first element to 90

Obtaining Length

int n = [Link]; // n is 10 for the literal array above

🔄 Traversing Arrays
All elements can be visited with a standard for loop using the index.

for (int i = 0; i < [Link]; i++) {


[Link](arr[i]);
}
📦 Arrays of Objects
Create the array, then instantiate each object and assign its reference.

Example: an array of five Student objects, each created with the Student constructor.

Student[] classList = new Student[5];


for (int i = 0; i < [Link]; i++) {
classList[i] = new Student(/* constructor args */);
}

⚠️ Common Runtime Error


ArrayIndexOutOfBoundsException
Thrown when an attempt is made to access an index that is negative or ≥ [Link].

int[] a = {1,2,3};
int x = a[3]; // triggers ArrayIndexOutOfBoundsException

📤 Passing and Returning Arrays in Methods


Passing an Array
Arrays are passed by reference; modifications inside the method affect the original
array.

int sum(int[] data) {


int total = 0;
for (int v : data) total += v;
return total;
}

Returning an Array
A method can create and return a new array.

int[] generateSequence(int n) {
int[] seq = new int[n];
for (int i = 0; i < n; i++) seq[i] = i + 1;
return seq;
}

📈 Performance & Memory Considerations


Aspect Advantage Disadvantage
Access time Constant‑time O(1) index –
lookup
Memory layout Predictable, contiguous Fixed size → possible waste
allocation or shortage
Type homogeneity Guarantees uniform element Cannot store mixed types
type without wrappers
Insertion / Deletion Simple at ends (if new array Costly in middle – requires
created) shifting elements
Management Straightforward because size No built‑in resizing; must
is known create new array to change
size

You might also like