Arrays in Java | Technical-arbaab

 Arrays in Java

Normally, Arrays are the collection of similar types of elements stored in a contiguous fashion with a single variable name instead of storing the same values separately, There are three types of arrays you can work with:

  • One dimensional array
  • Two dimensional array
  • Multi-dimensional array

1. One dimensional array

let us first see what is the syntax to define a one dimensional array -

  datatype variablename[] = {value1, value2, value3.......n values};   
                              or   
  datatype[] variablename = {value1, value2, value3.......n values};   

here, if you are using a string array the values should be in double quotes like this-

 datatype variablename = {"value1","value2", "value3".......n values};   

here, n doesn't mean you can store unlimited numbers actually Java uses an integer as an index to the array and the maximum integer store by JVM is 2^32 so you can store 2,147,483,647 elements in an array.

How to instantiate a one dimensional array-

Suppose you want to store the names of all avengers in an array called Avengers this is how you can do this-

 String avengers[] = {"Iron_Man","Captain_Marvel","peter","Dr.Banner","Captain_America"};  

here, string is the data type for telling the compiler "what type of data you are going to store in your array", Avengers is the variable name and two open and close square brackets is a notation for array. In the right hand side inside the two curly braces the string values are stored in double codes.
Similarly for an array of integers, suppose you want to store the multiple mobile numbers in a variable called contact_numbers you can do like this-

 int contact_numbers[] = {999999999,88888888};
 int evennumbers[] = {2,4,6,8};  


Another method to declare an array-

If you just want to declare the size of array and wants to give the values at run time you can declare in this manner too-

 String laptop_brands[] = new String[n];   
 int numbers[] = new int[n];   
 char letters[] = new char[n];   

here, n is the size of an array you can choose any according to you.

Access the elements of an array-

You can access the element of an array just by giving the index number in the square braces braces not clear don't worry see the example then definitely you will-

 String cars[] = {"laemborghini","audi","Range_Rover"};  
 System.out.println(cars[2]);  

you will see the Range Rover is printed on the console because it is stored on the second index of an array cars[].To get a more clear idea how values are stored in one dimensional array have a look on this picture of 1D array-




Accessing Java Array Elements using for Loop-

What if you want to print all the elements of an array it will not be printed like this -

  int fruit[] = {1,2,3};        
  System.out.println(fruit);   

here, a random value is printed in place of [1,2,3] if you try to output like this for this you have to use a for loop like this-

  int number[] = new int[] {1,2,3,4,5};   
       for(int i=0;i<number.length;i++)   
       {   
         System.out.print(number[i]+" ");   
      }   

2. Two-dimensional arrays-

let us first see what is the syntax to define a two dimensional array -

   int matrix[][] = {{value1, value2, value3},{value4, value5},{value6, value7}};  

here, value1, value2 and value3 are the elements in the first row of an array called matrix similarly value4 and value5 are the elements in the second row and value6 and value 7 are the elements in the third row of a matrix array.

How to instantiate a two dimensional array-

Suppose you want to store the elements of a matrix in an array called My matrix this is how you can do this-

 int mymatrix[][] = {{1,2,3},{4,5,6},{7,8,9}};  
 System.out.println(mymatrix[2][1]);//8 will be printed  

Changing an array element-

 int mymatrix[][] = {{1,2,3},{4,5,6},{7,8,9}};  
 System.out.println("before changing the element is "+mymatrix[1][1]);
 mymatrix [1][1] = 56;  
 System.out.println("after changing the element is "+mymatrix[1][1]);           

Output

 before changing the element is 5  
 after changing the element is 56  

printing the elements of a two dimensional array-

In order to loop over a 2D array, we first go through each row, and then again we go through each column in every row That's why we need two loopsnested in each other. let us see one example -

 int mymatrix[][] = {{1,2,3},{4,5,6},{7,8,9}};  
           for(int i=0;i<mymatrix.length;i++)  
           {  
                for(int j=0;j<mymatrix[i].length;j++)  
                {  
                     System.out.print(mymatrix[i][j]+"\t");  
                }  
                System.out.println();  
           }  

Output: 

 1     2     3       
 4     5     6       
 7     8     9       

remember to use only print in place of println otherwise the elements will not be printed like this.

problem 1- "print this amazing pattern by using 2D array"

problem statement- make a 2D array consist of 5 rows and 5 columns and in the first row only one element should be there and in the second row 2 elements should be there similarly in the 5th row 5 elements should be there and the elements should be in the form-

 0       
 1     2       
 3     4     5       
 6     7     8     9       
 10     11     12     13     14       

Source code-

 import java.io.IOException;  
 public class Array02 {  
      public static void main(String[] args) throws IOException  
      {  
           mydifferentArray();  
      }  
      public static void mydifferentArray()  
      {  
           int a[][] = new int[5][];  
           for(int i=0;i<a.length;i++)  
           {  
                a[i] = new int[i+1];//structure of a 2D array is ready  
           }  
           int c=0;//initialization with elements of a 2D array  
           for(int i=0;i<a.length;i++)  
           {  
                for(int j=0;j<a[i].length;j++)  
                {  
                     a[i][j]=c++;//post increment  
                }  
           }  
           for(int i=0;i<a.length;i++)//printing of an 2D array  
           {  
                for(int j=0;j<a[i].length;j++)  
                {  
                     System.out.print(a[i][j]+"\t");  
                }  
                System.out.println();  
           }  
      }  
 }  

Imp.note- Hey you don't be so confused by seeing the above code obviously you will find some difficulty understanding it "not as complex as you think" you can master these type of questions by hands on practice only you have to practice a lot in programming to handle more such questions easily so keep calm and move forward. I am again remembering you try to develop your own logic just look the problem statement and first try to solve it from your own and then see the solution I have just added this code because it's a beautiful pattern simply.

3. Multi dimensional arrays

Multidimensional arrays are basically array of arrays every element of array holds the reference to another array these are also known as Jagged Arrays It consists of 2D array, 3D array, 4D array etc.

 int my2Darray[][] = new int[][] {{1,2,3},{4,5,6},{7,8,9}};  
 int my3Darray[][][] = new int[][][]{{{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15},{16,17,18}}};  

To get a more clear idea about multidimensional arrays look at this picture-











Comments

Post a Comment

Hello, visitor your comments are so helpful for us so do not hesitate just write the comment we read all your comments so don't think your comment goes waste.