ARRAYS IN JAVA
Introduction to Arrays
What is an Array?
An array is a collection of elements of the same data type stored in
contiguous memory locations.
Key Points:
● Stores multiple values in one variable
● Same data type
● Fixed size
● Index starts from 0
● Stored in heap memory
Characteristics of Arrays
● Fixed size
● Same data type
● Index starts from 0
● Stored in contiguous memory
● Length cannot be changed
Declaration and Creation of Array
Method 1:
int[] arr;
Method 2:
int arr[];
✔ First one is preferred
Step 1: Declaration
int arr[];
Step 2: Creation
arr = new int[5];
Combined:
int arr[] = new int[5];
This creates:
● Size = 5
● Index = 0 to 4
Initialization
Method 1: Manual Assignment
arr[0] = 10;
arr[1] = 20;
Method 2: Direct Initialization
int arr[] = {10, 20, 30, 40};
Accessing and Traversing Array
Using for loop
for(int i = 0; i < [Link]; i++) {
[Link](arr[i]);
}
Using enhanced for loop
for(int value : arr) {
[Link](value);
}
Types of Arrays
One-Dimensional Array
int[] arr = {1,2,3};
One-Dimensional Array Example
Program: Find Sum and Average
class Test {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int sum = 0;
for(int i = 0; i < [Link]; i++) {
sum += arr[i];
}
double avg = (double)sum / [Link];
[Link]("Sum = " + sum);
[Link]("Average = " + avg);
}
}
Two-Dimensional Array (Matrix)
int[][] matrix = new int[2][3];
2 rows, 3 columns
Initialization
int[][] arr = {
{1,2,3},
{4,5,6}
};
Traversing 2D Array
for(int i = 0; i < [Link]; i++) {
for(int j = 0; j < arr[i].length; j++) {
[Link](arr[i][j] + " ");
}
[Link]();
}
Two-Dimensional Array Example
class Test {
public static void main(String[] args) {
int[][] arr = {
{1, 2, 3},
{4, 5, 6}
};
for(int i = 0; i < [Link]; i++) { // rows
for(int j = 0; j < arr[i].length; j++) { // columns
[Link](arr[i][j] + " ");
}
[Link]();
}
}
}
Ragged (Jagged) Array
What is Ragged Array?
A 2D array where each row has different number of columns.
Example Program
class Test {
public static void main(String[] args) {
int[][] arr = new int[3][];
arr[0] = new int[2];
arr[1] = new int[4];
arr[2] = new int[3];
arr[0][0] = 10;
arr[1][3] = 40;
for(int i = 0; i < [Link]; i++) {
for(int j = 0; j < arr[i].length; j++) {
[Link](arr[i][j] + " ");
}
[Link]();
}
}
}
✔ Rows have different sizes
✔ Allowed in Java
Array Copying
There are 3 common ways:
Using Loop
int[] original = {1,2,3,4};
int[] copy = new int[[Link]];
for(int i = 0; i < [Link]; i++) {
copy[i] = original[i];
}
Using clone()
int[] original = {1,2,3,4};
int[] copy = [Link]();
✔ Shallow copy
✔ Fast and easy
Using [Link]()
int[] original = {1,2,3,4};
int[] copy = new int[4];
[Link](original, 0, copy, 0, [Link]);
Parameters:
(source, sourcePos, dest, destPos, length)
Using [Link]()
import [Link];
int[] copy = [Link](original, [Link]);
Array Sorting
Using [Link]()
import [Link];
int[] arr = {5,2,8,1,9};
[Link](arr);
[Link]([Link](arr));
Output:
[1, 2, 5, 8, 9]
Sorting in Descending Order (Manual)
for(int i=0;i<[Link];i++){
for(int j=i+1;j<[Link];j++){
if(arr[i] < arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
Searching in Array
Linear Search
🔹 What is Linear Search?
Linear search checks each element one by one until the element is found.
Used when:
● Array is not sorted
● Small dataset
✅ Example Program
class Test {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int key = 30;
boolean found = false;
for(int i = 0; i < [Link]; i++) {
if(arr[i] == key) {
[Link]("Element found at index: " + i);
found = true;
break;
}
}
if(!found) {
[Link]("Element not found");
}
}
}
Arrays Utility Class
import [Link];
Common Methods:
Method Purpose
sort() Sort array
toString() Convert to
string
equals() Compare arrays
copyOf() Copy array
fill() Fill array
Example:
[Link](arr,
0);
Returning Array from a Method (Detailed)
🔹 Concept
A method can return an array just like it returns a single value.
Syntax:
returnType[] methodName() {
return arrayName;
}
✅ Example 1: Returning an Array
class Test {
static int[] createArray() {
int[] arr = {10, 20, 30, 40};
return arr;
}
public static void main(String[] args) {
int[] result = createArray();
for(int i = 0; i < [Link]; i++) {
[Link](result[i]);
}
}
}
Output
10
20
30
40
Array of Objects
🔹 Concept
When we create:
Student[] students = new Student[3];
This creates:
● Array that can hold 3 references
● But actual Student objects are NOT created yet
Initially:
students[0] = null
students[1] = null
students[2] = null
✅ Example Program
class Student {
int marks;
}
class Test {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student();
students[1] = new Student();
students[2] = new Student();
students[0].marks = 80;
students[1].marks = 70;
students[2].marks = 90;
for(int i = 0; i < [Link]; i++)
{
[Link](students[i].marks);
}
}
}
Output
80
70
90
Common Array Operations
✅ A) Find Sum
int[] arr = {10, 20, 30, 40};
int sum = 0;
for(int i = 0; i < [Link]; i++) {
sum += arr[i];
}
[Link]("Sum = " + sum);
✅ B) Find Maximum
int[] arr = {15, 45, 10, 89, 32};
int max = arr[0];
for(int i = 1; i < [Link]; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
[Link]("Maximum = " + max);
✅ C) Find Minimum
int min = arr[0];
for(int i = 1; i < [Link]; i++) {
if(arr[i] < min)
min = arr[i];
}
✅ D) Reverse Array (Printing Reverse)
int[] arr = {1, 2, 3, 4, 5};
for(int i = [Link] - 1; i >= 0; i--) {
[Link](arr[i] + " ");
}
Output:
5 4 3 2 1
Limitations of Arrays
● Fixed size
● Cannot resize
● Cannot store mixed types
● No built-in add/remove