
Play Store Application link – https://play.google.com/store/apps/details?id=com.ideepro.java25hours
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-
Concept | Description | Example |
---|---|---|
Declaration | Creating a reference variable that can hold a value of a specific data type | int[] arr; |
Instantiation | Creating an actual array object in memory using the new operator | arr = new int[10]; |
Initialization | Assigning 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]](https://corejava25hours.com/wp-content/uploads/2021/02/multi-dimensional-array-in-java1.jpg)
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)); } }
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)); } }
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)); } }
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)); } }
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)); } }
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 ?
This app is awesome