Java Arrays With Examples

Java Arrays With Examples


Java Array :

 An array is a group of variable of similar data type that shares a common name, and store in continuous memory location.

The size of the array refer how many element an arra can hold.

We can identify an array element by using array name and its index number.

One of the primary goals of Java is safety, so many of the problems that plague programmers in C and C++ are not repeated in Java. A Java array is guaranteed to be initialized and cannot be accessed outside of its range. The range checking comes at the price of having a small amount of memory overhead on each array as well as verifying the index at run time.

The first element index number is 0, and last element index is less then one of the size of the array.

In Simple words, We can say that an array is used to store multiple values under single name.

Types of Array :

Basically, array are of two types: 

    One-Dimensional-Array
    Multi-Dimensional-Array

One-Dimensional-array

An array that has one subscript is known as a one-dimensional array.They store data in the form of vector, or list.

A one-dimensional-array can be any type like - int, char, float, long, double, etc

Syntax To Declare one-Dimensional-Array -

 

DataType ArrayNmae [] = new DataType[size];

                                     OR

DataType [] ArrayNmae  = new DataType[size];

int [] a = new int[10];

int [] a = new int[10];

 

 Valid and invalid array creations-

1) int a[] = new int[10];// CORRECT
 

2) int a[] = new int[]; // WRONG
 

3) int a[];
    a =
new int[10]; //CORRECT

4)
int []a = new int[10];//CORRECT

5)
int p = 5;
  
int a[] = new int[p];// CORRECT

7)
int N;
    N = ob.nextInt();
   
int a[] = new int[N]; CORRECT

Length  

Return length of an array

Example -

Output -

How to Traverse an Array -

Output :

There is another way of traversing array in java as we have seen earlier in loops using for-each-loop.

This will produce the same output as  previous

Note-

  An array takes memory at runtime.

  An array is a static data structure its size can not grow and shrink.

 

Example - Sum of array elements

Output -

Example - write a program that reads two array and prrint the sum of corrosponding elements.

package javaLearnings;

public class Main {

  
public static void main(String[] args) {
 
// write your code here
       int[]a={3,5,2};

      
int b[]={5,3,6};

      
int c[]=new int[a.length];

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

       {

           c[i]=a[i]+b[i];
//ADDING

       }

       System.out.println(
"Sum of two array");

      
for( int j=0;j<=c.length-1;j++)

       {

           System.out.println(c[j]);
//DISPLAYING

       }

   }

Output -

 

Sum of two array
8
8
8

 

Multi-Dimensional-array

 

An array that has two subscripts is known as 2-D array.

They store data in the form of matrix, or table where first subscript represents a column.

You can easily create multidimensional arrays. For a multidimensional array of primitives, you delimit each vector in the array by using curly braces:

Syntax -

Valid / Invalid 2D array declarations -

Note -

In Java, each row can have an equal number of column or an unequal number of columns.

 

int twod [] [] = new int[4][];/*This is how We can have unequal number of column.*/
twod [0] = new int[1];//
twod [1] = new int[2];// It means row 1 will have 2 column
twod [2] = new int[3];//
 twod [3] = new int[4];//

 

Example - Write a program that read element in r*c matrix, and display them.

package javaLearnings;

import java.util.Scanner;

public class Main {

  
public static void main(String[] args) {
     
int r,c;
       Scanner ob =
new Scanner(System.in);
       System.out.println(
"enter Matrix row and column");
       r = ob.nextInt();
       c = ob.nextInt();
      
int a[][] = new int[r][c];
      
int i,j;
       System.out.println(
"Enter Matrix values");
      
for (i=0;i<r;i++){
          
for (j=0;j<c;j++){
               a[i][j] = ob.nextInt();
           }
       }
       System.out.println(
"Value of matrix are = ");
      
for (i=0;i<r;i++){
           
for (j=0;j<c;j++){
               System.out.print(a[i][j]);
           }
           System.out.println();
       }
   }
}

Sum of two array