💻Step 19:Array -12th hour + code

Learn with youtube video-

one-dimensional-array-in-java[1]

💡Definition-

array is a collection of similar type of elements.

It’s a static data Structure. //fixed size

Index of array starts from zero to size-1.

Phases of object creation-

ConceptDescriptionExample
DeclarationCreating a reference variable that can hold a value of a specific data typeint[] arr;
InstantiationCreating an actual array object in memory using the new operatorarr = new int[10];
InitializationAssigning values to the elements of the array using a loop, array literal, or method such as Arrays.fill()arr[i] = i; or int[] arr = {1, 2, 3, 4, 5}; or Arrays.fill(arr, 0);


1- Declaration– (Datatype and reference variable)
2-Instantiation– (memory allocation of certain size)
3-Initialization- (giving values into allocated memory)

💡(i)1-D Array
Case1💡

dataType arrRef [];                 //declaration

arrrRef=new dataType[size];             //instantiation

arrRef [zero] to arrRef [size-1] = someValue; //initialization

Case2-💡

dataType arrRef []=new dataType[size];     // declaration & instantiation

arrRef [zero] to arrRef [size-1] = someValue; //initialization

Case3-💡

*dataType arrRef []={ Value1, Value2……,ValueN }; //declaration,instatiation,initialization

💡(ii) Multidimensional

multi-dimensional-array-in-java[1]

Ex-

int [][] arr=new int [3][3];        //3 row and 3 column

arr[0][0]=1; to  arr[2][2]=n;

OR

int arr[][]={{1,2,3},
{2,4,5},
{4,4,5}};

Code samples are here

1-d array in case 1

package Array; 
import java.util.Arrays; 
public class ExArr1Case1 { 
public static void main(String[] args) { 
int[] abc; 
abc = new int[5]; 
abc[0] = 3; 
abc[1] = 7; 
abc[2] =12; 
abc[3] =9; 
abc[4] =2; 
System.out.println(Arrays.toString(abc)); 
} 
}

Run this Program ▶️

2- 1-d array in case 2

package Array; 
import java.util.Arrays; 
public class ExArr1Case2 { 
public static void main(String[] args) { 
int[] abc = new int[5]; 
abc[0] = 3; 
abc[1] = 7; 
abc[2] =12; 
abc[3] =9; 
abc[4] =2; 
System.out.println(Arrays.toString(abc)); 
} 
}
 
 
 
3- 1-d array in case 3
 
package Array; import java.util.Arrays; 
public class ExArr1Case3 { 
public static void main(String[] args) { 
int[] abc ={3, 7, 12, 9, 2}; 
System.out.println(Arrays.toString(abc)); 
} 
}
 
 
4- 2d array in case 1

 

package Array;

import java.util.Arrays;

public class ExArr2Case1 {

public static void main(String[] args) {

int[][] abc;

abc = new int[2][3];

abc[0][0] = 3;
abc[0][1] = 7;
abc[0][2] = 12;
abc[1][0] = 9;
abc[1][1] = 2;
abc[1][2] = 10;

System.out.println(Arrays.deepToString(abc));
}
}
 
 
5- 2d array in case 2

 

package Array;

import java.util.Arrays;

public class ExArr2Case2 {

public static void main(String[] args) {
int[][] abc = new int[2][3];

abc[0][0] = 3;
abc[0][1] = 7;
abc[0][2] = 12;
abc[1][0] = 9;
abc[1][1] = 2;
abc[1][2] = 10;

System.out.println(Arrays.deepToString(abc));
}
}
 
 
6- 2d array in case 3

 

package Array;

import java.util.Arrays;

public class ExArr2Case3 {

public static void main(String[] args) {
int[][] abc ={{3, 7, 12},{9, 2, 10}};

System.out.println(Arrays.deepToString(abc));
}
}

Run this Program 

Interview Questions —

  • what is array ?
  • Can we instantiate array without assigning size ?

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *