DEV Community

loading...

Arrays in Java| One D Arrays| 2 D Array |3 D Arrays

pankajkrravi profile image Pankaj Ravi Updated on ・3 min read

Implement One Dimensional Arrays in java

package com.ds.algorithms.array;

import java.util.Scanner;

/**

  • @Author pankaj
  • @create 09/04/21 5:14 PM
  • Note: Arrays are Fixed in Size
  • Array store only homogeneous element */ -------------------------------------------------- public class ImplementOneDArray { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("Plz Enter the Size of Array??"); int size=scanner.nextInt(); //code to create array with size, given by user int[] array=new int[size]; System.out.println("Plz Enter all "+size+" Value to store in Array"); //code to insert all input element from user for (int i=0;i< size;i++) { array[i]=scanner.nextInt(); } // code to print all arrays element System.out.println("All the values stored in Array is Following"); for (int i=0;i<size;i++) { System.out.println(array[i]); } } } --------------------------------------------------- # Implement two Dimensional Arrays in Java --------------------------------------------------------- package com.ds.algorithms.array;

import java.util.Scanner;

/**

  • @Author pankaj
  • @create 4/10/21 11:44 AM
    *
    In java multi dimensional array is implemented as array of arrays concept or jagged array
    Note: In multidimensional array we must need to specify base size, and remaining can be ignored.

    */

    public class ImplementThreeDArray {
    public static void main(String[] args)
    {
    /* #######################################
    int [][][] threeDArray =new int[2][][];
    threeDArray[0] =new int[3][];
    threeDArray[0][0]=new int[1];
    threeDArray[0][1]=new int[2];
    threeDArray[0][2]=new int[3];

    threeDArray[1]=new int[2][2];
    #############################################*/
    
    Scanner scanner=new Scanner(System.in);
    System.out.println("Plz Enter base siz 3D Array");
    int basSize1=scanner.nextInt();
    System.out.println("Plz Enter base size of  2D Array");
    int basSize2=scanner.nextInt();
    System.out.println("Plz Enter base size of  1D Array");
    int basSize3=scanner.nextInt();
    

    //create 3 D array
    int [][][] threeDArray=new int[basSize1][basSize2][basSize3];
    System.out.println("Please Enter all "+basSize1*basSize2*basSize3 +" values foe 3 D Array !!!!");
    for (int i=0;i<basSize1;i++)
    {
    for (int j=0;j<basSize2;j++)
    {
    for (int k=0;k<basSize3;k++)
    {
    threeDArray[i][j][k]=scanner.nextInt();
    }
    }
    }

    // call method to print values
    printThreeDArray(threeDArray);
    }

    public static void printThreeDArray(int [][][] array)
    {

    /* for (int i=0;i<array.length;i++)
     {
         for (int j=0;j<array[i].length;j++)
         {
             for ( int k=0;k<array[i][j].length;k++)
             {
                 System.out.print(array[i][j][k] + " ");
             }
         }
         System.out.println();
     } */
     for( int [][] i:array)
     {
         for(int [] j: i)
         {
             for( int k: j)
             {
                 System.out.print(k+" ");
             }
             System.out.println();
         }
         System.out.println();
     }
    

    }

    }

    code to Add Two Dimensional Array || Add two Matrix


    package com.ds.algorithms.array;

/**

  • @Author pankaj
  • @create 09/04/21 6:28 PM */

public class AdditionOfTwoDMertices {
public static void main(String[] args) {
//array 1
int [][] array1={
{2,2,2},
{3,3,3},
{4,4,4,}
};
//array 2
int [][] array2={
{2,2,2},
{5,5,5},
{6,6,6,}
};
System.out.println("Print array first");
ImplementTwoDArray.printTwoDArray(array1);
System.out.println("print array second");
ImplementTwoDArray.printTwoDArray(array2);
// create new array to store addition of array
int [] [] resultArray=new int[array1.length][array1.length];
// logic for addition
for (int i=0;i<array1.length;i++)
{
for (int j=0;j<array1[i].length;j++)
{
resultArray[i][j]=array1[i][j]+array2[i][j];
}
}
// call method to print resultant array
System.out.println(" ####### Print resultant array #############");
ImplementTwoDArray.printTwoDArray(resultArray);
}

}

Implement Three Dimensional Arrays


package com.ds.algorithms.array;

/**

  • @Author pankaj
  • @create 09/04/21 5:51 PM
    */
    public class ImplementTwoDArray {
    public static void main(String[] args) {
    //way 1
    int [][] empSalary={
    //emp sal department wise,sales,testing,dev
    // 1 1 3 4 5
    {44444,654,897,896347,7478456},
    {5845,34,234,3645,876,78687,4545,435535},
    {56645566,654646,4564666,12578,45564,3434,34435,5342,234}
    };
    // print all emp salary
    System.out.println("+++++++++++++ print all emp salary ++++++++++++++++");
    printTwoDArray(empSalary);
    // way 2
    int [][] twoDArray=new int[3][4];
    // 3 one D array with 4 elements
    System.out.println("========== print 2d array with default values==========");
    printTwoDArray(twoDArray);

    }
    public static void printTwoDArray(int [][] array){
    for (int i = 0; i < array.length; i++) // 0 to 2
    {
    for (int j = 0; j < array[i].length; j++) {
    System.out.print(array[i][j] + " ");
    }
    System.out.println();
    }
    }
    }


---------------welcome for your suggestions --------------

Discussion (0)

pic
Editor guide