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

Array in Java

An array in Java is a fixed-size collection of elements of the same data type, accessed via an index. It can store both primitive types and objects, with various types including 1D, 2D, and multi-dimensional arrays. Common operations include declaration, creation, accessing elements, modifying values, and looping through the array.

Uploaded by

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

Array in Java

An array in Java is a fixed-size collection of elements of the same data type, accessed via an index. It can store both primitive types and objects, with various types including 1D, 2D, and multi-dimensional arrays. Common operations include declaration, creation, accessing elements, modifying values, and looping through the array.

Uploaded by

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

Array in Java

Definition

An array in Java is a fixed-size collection of elements of the same data type, stored in
contiguous memory locations, accessed using an index.

Key Characteristics

 Stores multiple values of the same type in a single variable.


 Size is fixed once declared (cannot grow or shrink).
 Index starts at 0 and ends at length – 1.
 Can store primitive types (int, char, double…) or objects (String, classes).

Types of of Array

Type Description Example


1D Array Linear list int[] a = {1,2,3};

2D Array Rows and columns int[][] b = {{1,2},{3,4}};

Multi-Dimensional More than 2 levels int[][][] c;

Primitive Array Stores primitive values float[] f;

Object Array Stores objects String[] s;

Real-Life Usage Examples

Use Case Array Type


List of student ages 1D Primitive
Student names 1D Object
Timetable (Mon–Fri × Subjects) 2D
3D Tic–Tac–Toe Game 3D
RGB color values in images Multidimensional

Declaring and Creating Arrays

int[] numbers; // Declaration

numbers = new int[5];

Combined declaration and creation


int[] age = new int[3];

With values:

String[] courses = {"Java", "Python", "SQL"};

Accessing Array Elements


int[] marks = {90, 85, 75};

[Link](marks[0]); // Output: 90

Modifying elements

marks[1] = 88;

Array Length Property


int lengths = [Link];

[Link]("Total items: " + lengths);

Loops with Arrays

for loop:

int[] nums = {2, 4, 6, 8};

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

[Link](nums[i]);

Using foreach

for(int n : nums){

[Link](n);

for-each cannot modify values directly.

Examples 1

Sum of Elements

int[] numbers = {5, 10, 15, 20};

int sum = 0;

for(int n : numbers){
sum += n;

[Link]("Sum = " + sum);

Multidimensional Array (2D Array)

Declaration & creation:

int[][] matrix = new int[2][3]; // 2 rows, 3 columns

With values:

int[][] table = {

{1, 2, 3},

{4, 5, 6}

};

Looping through 2D array:

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

for(int j = 0; j < table[i].length; j++){

[Link](table[i][j] + " ");

[Link]();

Common Array Operations

Operation Example
Create int[] a = new int[5];

Assign a[0] = 10;

Access a[2]

Find length [Link]


Loop for(int x : a)

You might also like