# 💻Step 19:Array -12th hour + code

Learn with youtube video-

### 💡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-

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

#### 💡(i)1-D ArrayCase1–💡

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

#### 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));
}
}
```

## Interview Questions —

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

## One comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.