Jav Array
Jav Array
String[] cars;
Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure
where we store similar elements. We can store only a fixed set of elements in a Java
array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.
In Java, array is an object of a dynamically generated class. Java array inherits the
Object class, and implements the Serializable as well as Cloneable interfaces. We can
store primitive values or objects in an array in Java. Like C/C++, we can also create
single dimentional or multidimentional arrays in Java.
Moreover, Java provides the feature of anonymous arrays which is not available in
C/C++.
Advantages
o Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
o Random access: We can get any data located at an index position.
Efficient Access: Accessing an element by its index is fast and has constant time
complexity, O(1).
Memory Management: Arrays have fixed size, which makes memory management
straightforward and predictable.
Data Organization: Arrays help organize data in a structured manner, making it easier
to manage related elements.
o
Disadvantages
o Size Limit: We can store only the fixed size of elements in the array. It doesn't grow
its size at runtime. To solve this problem, collection framework is used in Java which
grows automatically.
o Type Homogeneity: Arrays can only store elements of the same data type, which
may require additional handling for mixed types of data.
o Insertion and Deletion: Inserting or deleting elements, especially in the middle of
an array, can be costly as it may require shifting elements.
Output
10
20
70
40
50
Output:
33
3
4
5
System.out.println(min);
}
10
22
44
66
Returning Array from the Method
We can also return an array from the method in Java.
10
30
50
90
60
ArrayIndexOutOfBoundsException
The Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException if length
of the array in negative, equal to the array size or greater than the array size while
traversing the array.
Output:
class GFG {
int[] arr;
arr[0] = 10;
arr[1] = 20;
// so on...
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
}
Output
Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50
// an array of objects
class Student {
this.roll_no = roll_no;
this.name = name;
Student[] arr;
// so on...
+ arr[i].name);
OUTPUT
Output
Element at 0 : 1 aman
Element at 1 : 2 vaibhav
Element at 2 : 3 shikar
Element at 3 : 4 dharmesh
Element at 4 : 5 mohit
arr[0] = 10;
arr[1] = 20;
System.out.println(arr[i]);
}
Output
10
20
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
Let's see the simple example to declare, instantiate, initialize and print the 2Dimensional
array.
//Java Program to illustrate the use of multidimensional array
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}
Output:
123
245
445
Output:
268
6 8 10
666
12 12 12
18 18 18
OUTPUT
8
Write a program in Java to enter natural numbers in a double
dimensional array m x n (where m is the number of rows and n
is the number of columns). Display the new matrix in such a
way that the new matrix is the mirror image of the original
matrix.
8 15 9 18
1
9 7 6
0
1
8 11 13
0
12 16 17 19
Sample Input
1
9 15 8
8
1
6 7 9
0
1
13 11 8
0
19 17 16 12
Sample Output
import java.util.Scanner;
System.out.println("Input Array:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
5 3 2 1
5 3 6 4
81 8 31 1
6 0
5 6
17 12
8 4
2 1 2 2
2 4 3 5
Sample Output
2 1 2 2
2 4 3 5
8 1
81 31
6 0
5 6
17 12
8 4
5 3 2 1
5 3 6 4
ANSWER
import java.util.Scanner;
System.out.println("Input Array:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
System.out.println("Swapped Array:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
OUTPUT
Java Program to Rotate Matrix Elements
// Java Program to Rotate Matrix Elements
import java.lang.*;
import java.util.*;
// main Class
class GFG {
static int r = 4;
static int c = 4;
// Method
// To rotate a matrix of
// p = r and q = c
int rw = 0, cl = 0;
// x is the iterator
if (rw + 1 == p || cl + 1 == q)
break;
current = matrix[rw][x];
matrix[rw][x] = previous;
previous = current;
rw++;
matrix[x][q - 1] = previous;
previous = current;
q--;
if (rw < p) {
previous = current;
p--;
if (cl < q) {
current = matrix[x][cl];
matrix[x][cl] = previous;
previous = current;
cl++;
System.out.print("\n");
// Method 2
int b[][] = { { 5, 6, 7, 8 },
{ 1, 2, 3, 4 },
{ 0, 15, 6, 5 },
{ 3, 1, 2, 12 } };
rotate_matrix(r, c, b);
Output
0 15 2 8
3634
1 2 12 5
2 3 5 7 11
2 2
13 17 19
3 9
3 4 4 4
31
7 1 3 7
5 5 6
61 71
3 9 7
import java.util.Scanner;
if (div == 2) {
arr[r][c++] = i;
count++;
if (c == n) {
r++;
c = 0;
}
}
Output
2 3 5
7 11 13
17 19 23
---------------------------------------
Element | Frequency
---------------------------------------
1 | 2
2 | 4
8 | 1
3 | 1
5 | 1
----------------------------------------