Java Arrays
An array is a collection of similar type of elements which has contiguous memory location.
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.
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.
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.
Types of Array in java
There are two types of array.
o Single Dimensional Array
o Multidimensional Array
1) Single Dimensional Array in Java
An array that represent in a single dimensional is called single dimensional array
1. dataType[] arr; (or)
2. dataType []arr; (or)
3. dataType arr[];
Instantiation of an Array in Java
1. //Java Program to illustrate how to declare, instantiate, initialize
2. //and traverse the Java array.
3. class Testarray{
4. public static void main(String args[]){
5. int a[]=new int[5];//declaration andinstantiation
6. a[0]=10;//initialization
7. a[1]=20;
8. a[2]=70;
9. a[3]=40;
10. a[4]=50;
11. //traversing array
12. for(int i=0;i<a.length;i++)//length is the property of array
13. System.out.println(a[i]);
14. }}
Test it Now
Output:
10
20
70
40
50
Declaration, Instantiation and Initialization of Java Array
We can declare, instantiate and initialize the java array together by:
1. int a[]={33,3,4,5};//declaration, instantiation and initializationLet's see
2. the simple example to print this array.
1.
2. //Java Program to illustrate the use of declaration, instantiation
3. //and initialization of Java array in a single line
4. class Testarray1{
5. public static void main(String args[]){
6. int a[]={33,3,4,5};//declaration, instantiation and initialization
7. //printing array
8. for(int i=0;i<a.length;i++)//length is the property of array
9. System.out.println(a[i]);
9. }Test it Now
.
2) Multidimensional Array in Java
An array that represent in two dimension i.e. row and column based index is called matrix.
Syntax to Declare Multidimensional Array in Java
1. datatype array name[][] ;
2. datatype array nane [][];
Example to instantiate Multidimensional Array in Java
1. int a[][] =new int[3][3]; //3 row and 3 column
Example to initialize Multidimensional Array in Java
1. arr[0][0]=1;
2. arr[0][1]=2;
3. arr[0][2]=3;
4. arr[1][0]=4;
5. arr[1][1]=5;
6. arr[1][2]=6;
7. arr[2][0]=7;
8. arr[2][1]=8;
9. arr[2][2]=9;
Example of Multidimensional Java Array
Let's see the simple example to declare, instantiate, initialize and print the 2Dimensionalarray.
1. //Java Program to illustrate the use of multidimensional array
2. class Testarray3{
3. public static void main(String args[]){
4.
5. //declaring and initializing 2D array
5.
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
int I, j;
6. //printing 2D array 7.
for( i=0;i<3;i++){
8. for( j=0;j<3;j++){
9. System.out.print(arr[i][j]+" ");
10. }
11. System.out.println();
12. }
13. }}
Test it Now
Output:
1 2 3
2 4 5
4 4 5
11. for(int i:carr)
12. System.out.println(i);13.
14. System.out.println("Are both equal?");
15. System.out.println(arr==carr);16.
17. }}
Output:
Printing original array:
33
3
4
5
Printing clone of the array:
33
3
4
5
Are both equal?
false
Addition of 2 Matrices in Java
Let's see a simple example that adds two matrices.
1. //Java Program to demonstrate the addition of two matrices in Java
2. class Testarray5{
3. public static void main(String args[]){
4. //creating two matrices
5. int a[][]={{1,3,4},{3,4,5}};
6. int b[][]={{1,3,4},{3,4,5}};
7.
8. //creating another matrix to store the sum of two matrices
9. int c[][]=new int[2][3];
10.
11. //adding and printing addition of 2 matrice
12. for(int i=0;i<2;i++){
13. for(int j=0;j<3;j++){
14. c[i][j]=a[i][j]+b[i][j];
15. System.out.print(c[i][j]+" ");
16. }
17. System.out.println();//new line
18. }
19.
20. }}
Test it Now
Output:
2 6 8
6 8 10
Multiplication of 2 Matrices in Java
In the case of matrix multiplication, a one-row element of the first matrix is multiplied by all the columns of the
second matrix which can be understood by the image given below.
Let's see a simple example to multiply two matrices of 3 rows and 3 columns.
1. //Java Program to multiply two matrices
2. public class MatrixMultiplicationExample{
3. public static void main(String args[]){
4. //creating two matrices
5. int a[][]={{1,1,1},{2,2,2},{3,3,3}};
6. int b[][]={{1,1,1},{2,2,2},{3,3,3}};
7. int I,j,k;
8. //creating another matrix to store the multiplication of two matrices
9. int c[][]=new int[3][3]; //3 rows and 3 columns
10.
11. //multiplying and printing multiplication of 2 matrices
12. 12. for( i=0;i<3;i++){
13. for(j=0;j<3;j++){
14. c[i][j]=0;
15. for(k=0;k<3;k++)
16. {
17. c[i][j]=c[i][j]+a[i][k]*b[k][j];
18. }
19. System.out.print (c[i][j]+" ");
20. }
21. System.out.println();
22. }
23. }}
Test it Now
Output:
6 6 6
12 12 12
18 18 18
Related Topics
1) Java Program to copy all elements of one array into another array
2) Java Program to find the frequency of each element in the array
3) Java Program to left rotate the elements of an array
4) Java Program to print the duplicate elements of an array
5) Java Program to print the elements of an array
6) Java Program to print the elements of an array in reverse order
7) Java Program to print the elements of an array present on even position
8) Java Program to print the elements of an array present on odd position
9) Java Program to print the largest element in an array
10) Java Program to print the smallest element in an array
11) Java Program to print the number of elements present in an array
12) Java Program to print the sum of all the items of the array
13) Java Program to right rotate the elements of an array
14) Java Program to sort the elements of an array in ascending order
15) Java Program to sort the elements of an array in descending order
16) Multiply Two Matrices
17) Print Odd and Even Number from an Array
18) Transpose matrix
19) Java Program to subtract the two matrices
20) Java Program to determine whether a given matrix is an identity matrix
21) Java Program to determine whether a given matrix is a sparse matrix
22) Java Program to determine whether two matrices are equal
23) Java Program to display the lower triangular matrix
24) Java Program to display the upper triangular matrix
25) Java Program to find the frequency of odd & even numbers in the given
matrix
26) Java Program to find the product of two matrices
27) Java Program to find the sum of each row and each column of a matrix
28) Java Program to find the transpose of a given matrix