0% found this document useful (0 votes)
10 views

Array

The document provides an overview of arrays in Java, explaining their structure, features, and types, including single-dimensional and multi-dimensional arrays. It covers array declaration, initialization, and accessing elements, along with various problems and solutions related to array manipulation, such as printing elements, finding sums, and rotating arrays. Additionally, it includes example code snippets for practical understanding.

Uploaded by

mithunmaharaj597
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)
10 views

Array

The document provides an overview of arrays in Java, explaining their structure, features, and types, including single-dimensional and multi-dimensional arrays. It covers array declaration, initialization, and accessing elements, along with various problems and solutions related to array manipulation, such as printing elements, finding sums, and rotating arrays. Additionally, it includes example code snippets for practical understanding.

Uploaded by

mithunmaharaj597
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
You are on page 1/ 16

ARRAY

An Array in Java is a data structure that allows you to


store multiple values of the same type in a single
variable. It is a collection of variables that are accessed
using an index, which is a way to organize data.
Why Array ?
 They allow storing multiple values in a single
variable.
 They provide quick access to data through indices.
 They are efficient in memory and access when
handling large amounts of data.
Features of Arrays
 Fixed size: The size of an array is fixed once it is
created.
 Indexed: Array elements are accessed using an
index, starting from 0.
 Homogeneous: All elements in an array must be of
the same data type.
1. Declaration of Array
dataType[] arrayName;
OR
dataType arrayName[];
~ Example:
int[] numbers;

2. Initialization of Array
arrayName = new dataType[size];
~ Example:
numbers = new int[5];

3. Declaration + Initialization
dataType[] arrayName = new dataType[size];
~ Example:
int[] marks = new int[3];
int[] marks = { 85, 90, 95 };

4. Accessing Array Elements


System.out.println(marks[0]); // Output: 85
NULL ARRAY
A null array refers to an array that has been
declared but not initialized. It means the array does
not point to any memory location yet.
int[] arr = null;

TYPES OF ARRAY
1. Single-Dimensional Array: A single list of
elements.
2. Multi-Dimensional Array: Arrays with more
than one dimension (like 2D arrays).

1. Single-Dimensional Array
 A single-dimensional array is like a list of elements
of the same data type.

Syntax
dataType[] arrayName = new dataType[size];

Example:
int[] numbers = new int[5];
numbers[0] = 10;
numbers[1] = 20;
System.out.println(numbers[0]); // Output: 10
2. Multi-Dimensional Array
A multi-dimensional array is an array of arrays.
It can represent data in a matrix form (e.g., 2D
arrays).

Syntax
dataType[][] arrayName = new dataType[rows][columns];

Example

int[][] matrix = new int[3][3];


matrix[0][0] = 1;
matrix[0][1] = 2;
System.out.println(matrix[0][0]); // Output: 1
System.out.println(matrix[0][1]); // Output: 2

Initializing and Printing a 2D Array

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

for (int i = 0; i < matrix.length; i++) {


for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
Output
123
456
789

PROBLEMS:

1.Print array elements from left to right

public class LeftToRight {


public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 6};

System.out.println("Array from left to right:");


for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Output : 5 2 9 1 6
2. Print array elements from right to left
public class RightToLeft {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 6};

System.out.println("Array from right to left:");


for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
Output: 6 1 9 2 5

3. Sum of all elements

public class SumOfArray {


public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 6};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println("Sum = " + sum);
}
}
Output: Sum = 23
4. Smallest element in array

public class Smallest {


public static void main(String[] args) {
int[] a = {5, 2, 9, 1, 6};
int small = a[0];

for(int i = 1; i < a.length; i++) {


if(a[i] < small)
small = a[i];
}

System.out.println("Smallest: " + small);


}
}

Output: Smallest: 1

5. Largest element in array

public class Largest {


public static void main(String[] args) {
int[] a = {5, 2, 9, 1, 6};
int big = a[0];

for(int i = 1; i < a.length; i++) {


if(a[i] > big)
big = a[i];
}
System.out.println("Largest: " + big);
}
}

Output: Largest: 9

6. Swap 2 elements (pos1 and pos2)

public class Swap {


public static void main(String[] args) {
int[] a = {5, 2, 9, 1, 6};
int pos1 = 1, pos2 = 3; //swapping
int temp = a[pos1];
a[pos1] = a[pos2];
a[pos2] = temp;

System.out.print("After Swap: ");


for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}

Output : After Swap: 5 1 9 2 6


7. Insert an element in an array

public class InsertElement {


public static void main(String[] args) {
int[] a = {10, 20, 30, 40};
int pos = 2, ele = 99
int[] newArr = new int[a.length + 1];
// New array with extra space

// Copy elements before the inserting


for (int i = 0; i < pos; i++)
newArr[i] = a[i];

// Insert the new element


newArr[pos] = ele;

// Copy remaining elements after insertng


for (int i = pos; i < a.length; i++)
newArr[i + 1] = a[i];

System.out.print("After Insertion: ");


for (int i : newArr)
System.out.print(i + " ");
}
}

Output : After Insertion: 10 20 99 30 40


8. Delete an element in an array

public class DeleteElement {


public static void main(String[] args) {
int[] a = {10, 20, 30, 40};
int pos = 2; // to be deleted

// Create a new array with one less element


int[] newArr = new int[a.length - 1];

// Copy elements before deleting


for (int i = 0; i < pos; i++) {
newArr[i] = a[i];
}

// Copy elements after the deleting


for (int i = pos + 1; i < a.length; i++) {
newArr[i - 1] = a[i];
}

System.out.print("After Deletion: ");


for (int i = 0; i < newArr.length; i++) {
System.out.print(newArr[i] + " ");
}
}
}

Output : After Deletion: 10 20 40


9. Count even and odd elements in an array

public class EvenOddCount {


public static void main(String[] args) {
int[] a = {10, 21, 30, 43, 55};
int even = 0, odd = 0;

for (int i : a) {
if (i % 2 == 0)
even++;
else
odd++;
}

System.out.println("Even = " + even);


System.out.println("Odd = " + odd);
}
}

Output : Even = 2
Odd = 3

10. Reverse an array

public class ReverseWithoutTwoPointers {


public static void main(String[] args) {
int[] a = {10, 20, 30, 40, 50};
int[] rev = new int[a.length];

for (int i = 0; i < a.length; i++) {


rev[i] = a[a.length - 1 - i];
}

System.out.print("Reversed (New Array): ");


for (int i = 0; i < rev.length; i++) {
System.out.print(rev[i] + " ");
}
}
}
Output : 50,40,30,20,10

11. Reverse an array (Using two-pointer approach)

public class ReverseArray {


public static void main(String[] args) {
int[] a = {10, 20, 30, 40, 50};
int i = 0, j = a.length - 1;
// Swap elements from both ends of the array
while (i < j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
System.out.print("Reversed Array: ");
for (int k : a)
System.out.print(k + " ");
}
}
Output : 50,40,30,20,10

12. Find the second largest element

public class SecondLargest {


public static void main(String[] args) {
int[] a = {10, 50, 30, 40, 20, 60};
int max1, max2;

if(a[0] > a[1]) {


max1 = a[0];
max2 = a[1];
} else {
max1 = a[1];
max2 = a[0];
}

for(int i = 2; i < a.length; i++) {


if(a[i] > max1) {
max2 = max1;
max1 = a[i];
}
else if(a[i] > max2 && a[i] != max1) {
max2 = a[i];
}
}

System.out.println("Second Largest: " +


max2);
}
}
Output : Second Largest: 50

13. Rotate the array (left and right)

Left Rotate (by 1 position)

public class LeftRotate {


public static void main(String[] args) {
int[] a = {10, 20, 30, 40};
int first = a[0];

// Left shift all elements by 1


for (int i = 0; i < a.length - 1; i++)
a[i] = a[i + 1];
a[a.length - 1] = first; //puts at last
System.out.print("Left Rotated: ");
for (int i : a)
System.out.print(i + " ");
}
}
Output : Left Rotated: 20 30 40 10

Right Rotate (by 1 position)

public class RightRotate {


public static void main(String[] args) {
int[] a = {10, 20, 30, 40};
int last = a[a.length - 1];

// Right shift all elements by 1


for (int i = a.length - 1; i > 0; i--)
a[i] = a[i - 1];

// Put the last element at front


a[0] = last;
System.out.print("Right Rotated: ");
for (int i : a)
System.out.print(i + " ");
}
}
Output : Right Rotated: 40 10 20 30

You might also like