Array
Array
INDEX
Definition : 2
Types of array 2
es
Update/Add, Output 4
Length Function 4
ot
Enhanced for loop / for - each loop 5
Limitations of for-each loop 5
N
Two Dimensional array 6
Update/Add, Output 6
a
Methods in arrays 7
av
Advantages and Dis-Advantages of array 9
J
al
w
is
Ja
sh
iri
Sh
Definition :
Array is a collection of the same datatype (int, char, String etc) element stored in a data
structure of fixed size.
● Contiguous memory location
● Only Homogeneous data can be stored
● Indexing of the array start from 0
es
● Only Index based accessing is allowed
● It is a static data structure. Once the size of an array is declared it cannot be changed.
● All the elements are stored under one variable name
ot
Contiguous means it needs memory adjacent to each other.
N
a
J av
al
Types of array
w
2. Two-Dimensional Arrays
It is array of array in which elements are stored in continuous rows and columns
sh
Note:
● stack will store the reference of the object
Sh
● reference will store the memory address of the heap where the object is created
● Objects will be created in the heap
es
2) int [] arr2 = new int [3]; // 3 is the size of array
ot
N
a
Memory addresses are increasing by 4.
J
→ 400
av → 404 → 408
al
Because the datatype of the array is int which takes 4 bytes of memory.
arr1 is referring to 400 (it can be anything) because the first element of the array is on memory
w
address 400.
is
Values of arr2 are 0 that means these are default values. Default values will be allocated if we do
not add any value.
Ja
You can also update the value of the array of specific index
arr1[0] = 5;
arr1[1] = 6;
arr1[2] = 7;
Data Type Default Value Data Type Default Value Data Type Default Value
es
byte 0 short 0 int 0
long 0L float 0.0f double 0.0d
ot
char '' boolean false Object null
N
Update/Add, Output
If we update the values like we updated in the above example of arr1 then it will take a long time
a
to update the whole array. So we can use for loop to update the value
av
int [] arr2 = new int [3];
for (int i = 0; i < 3; i++) {
arr2 [i] = sc.nextInt();
J
}
al
w
As the size of the array is 3. But indexing in array start from 0 so, it will travel up-to size - 1 in this
case that is 3 - 1 = 2, so we have used <3
is
Length Function
Ja
Many times you may not know the size of an array. So to find out the size of an array we have a
.length function which will return int value.
sh
Now we know how to add or update all the values in an array in one go.
By using a for loop we can provide output. Just by changing the body with S.o.p (arr2[i]); line.
Syntax :
for (dataType variable : array reference variable) {
es
// body of the loop
}
ot
● array - an array or a collection
● variable - each item of array/collection is assigned to this variable
N
● dataType - the data type of the array/collection
a
System.out.println (num);
av
}
The value at indexes will be stored inside the variable num and it will be printed.
J
Limitations of for-each loop
al
● Not appropriate when modifying the array
w
es
int [][] arr3 = { // 0 1 3 index of column
{ 1, 2, 3 }, // 0 row index
{ 4, 5, 6 }, // 1 row index
ot
{ 7, 8, 9} // 2 row index
N
};
We can now clearly see that a 2d array is the array which contains arrays within itself.
a
int [row][column]
Update/Add, Output
If we want to update the values
inside a 2D array we need to specify
the index of row and column of the
J av
al
element that means row is the
element of the main array and
w
arr3 [0][0] = 3;
Ja
System.out.println (arr3[0][0]);
Output : 1
iri
When we don't have preknown values we need to add the size of row and column of the array
Sh
Syntax:
This means we have length of an array arr4 is 3 and the length of its element array is 3.
Methods in arrays
int [] arr1 = {1, 2, 3};
int [] arr2 = {1, 2, 3};
int [] arr3 = {4, 5, 6};
es
● == operator & .equals() method - NOT to USE in case of Array.
System.out.println (arr1 == arr2 ); Output : false
ot
Even though the values of the array are the same, it's giving false output.
Because == operator will check the references of arrays which store the memory address.
N
If the reference variables are pointing to the same memory location then output is true.
For above output to be true then the snippet should be
a
int [] arr1 = {1, 2, 3};
av
int [] arr2;
arr2 = arr1;
System.out.println (arr1 == arr2 );
J Output : true
al
In the above snippet both the references arr1 and arr2 are pointing toward the same
memory location so the output is true.
w
is
.equals() method
System.out.println (arr1.equals(arr2)); Output : false
Ja
This method is an Object class method which compares actual values. But in the case of an array
this method is overridden and it's the same as == operator checking the references of the arrays.
sh
es
● Arrays.sort (obj1)
This will sort the array in ascending order.
ot
● Arrays.toString (obj1)
N
Returns a string representation of the contents of the specified array.
a
Finds and returns the index of the first unmatched element between the two specified
av
arrays. And if both the arrays are the same it returns -1.
System.out.println (Arrays.mismatch (arr1, arr2)); output : -1
J
System.out.println (Arrays.mismatch (arr1, arr3)); output : 0
al
● Arrays.fill(obj, from index, to Index, value)
Used to Fill an array to specific value at each index
w
es
● Accessing an element is very fast because of contiguous memory and easy by using
the index number
ot
● Array allows random access to elements
● Arrays allow matrix data to be stored using a multidimensional array.
N
Dis-Advantages
a
● To create an Array, contiguous memory is required.
av
● The values stored in the array should have a homogeneous data type.
● The Array is a static data structure. It is of fixed size. We can not increase or decrease
J
the size of the Array after creation
al
● When it comes to insertion and deletion it is a bit difficult because elements are
w