Lecture 4.1 - Array new
Lecture 4.1 - Array new
1. Array
2. 1D Array Declaration
3. 2D Array Declaration
4. Accessing array elements
5. Array of Objects
6. Manipulating an Array of Objects
Array
What is Array?
Lets declare, initialize and draw the memory representation of an integer value.
The variable age can store only one value representing the age of only one person.
What if we need to store the age of lets say 40 persons? We will certainly not use 40
variables naming age1, age2, age3, . . . . , age40.
So, the array named arr1 can store only integer values and the size of this array is 5.
Also, it can be noted that the [ ] symbol (better known as Array Notation) can be placed
both before and after the name of the array.
Array
Memory Representation and Initialization of an Array: 1st Approach
1st Approach • This approach is used when we know the size of the
array but we do not know the elements of the array.
int arr1[ ] = new int [5]; • As we do not know the elements we can not initialize
Or, the array yet.
int [ ]arr1 = new int [5];
2nd Approach
• This approach is used when we neither know the size nor
int [arr2[ ];
]arr2; the elements of the array.
. • So, initially we can not allocate memory for the array.
. • Memory allocation is done after knowing the value of
. size.
arr2 = new int [size];
Here, Again, the Array Notation
• can be placed both before
int is the type of array.
and after the name of the
• arr2 is the name of the array.
array.
• The [ ] symbol denotes that it is an array, not a variable.
• size is the size of array, which was computed
somewhere in between the two statements.
Array
Memory Representation and Initialization of an Array: 2nd Approach
3rd Approach
arr3 11 22 33
15 44
int arr3[ ] = new int [ ] {11,22,33,44}; 0 1 2 3
4th Approach
arr4 11 22 15 44 55
0 1 2 3 4
Array
Printing array elements
1st Approach
int arr5[ ][ ] = new int [3][3];
Column index
As, no elements has been initialized, all the index
positions will be initialized by the default value 0. 0 1 2
0 0
11 0 0
Row index
Now, if we want to initialize a value in a particular index,
we can do it like this: 1 0 0 0
18
2 0 0
20 0
arr5 [0][0] = 11; arr5
arr5 [1][2] = 18;
arr5 [2][1] = 20;
Array
Memory Representation of 2D Array
2nd Approach:
Row index
arr6 = new int [row][col]; 0 11
0 0 0
3rd Approach
int arr7 [ ][ ] = new int [ ][ ]{{1,2}, {3,4}};
int [ ][ ]{{1,2}, {3,4}} denotes that there are two rows
as there are two pairs of inner curly braces and there 0 1
are two columns as each pair of inner curly braces have 0 11 2
1
two elements.
1 3 18
4
Now, if we want to initialize a value in a particular
index, we can do it like this:
Lets assume we have created an object of Box using its length 1.5
parameterized constructor and we have an array of Box. b1
width 1.2
Lets Assume that we have 3 objects of box class and an array length 1.5
of Box class. b1
width 1.2
boxes null null null height 1.3
0 1 2 length 2.5
We want to assign the objects in the array. How? b2
width 2.2
Solution: We can only assign these objects in the array if there height 2.3
is null value in any of the indexes. The followings steps can be
followed to assign an object in the array: length 3.5
• Start from the 1st index of the array and check the value of b3
width 3.2
that index.
• If it is null, assign the object and exit. Else, go to the next height 3.3
index.
• Repeat until the last index.
Array
Manipulating an Array of Objects
• Start from the 1st index of the array and check the value of that
length 1.5
index. b1
• If it is null, assign the object and exit. Else, go to the next index. width 1.2
• Repeat until the last index. height 1.3
Is Is Is
this this
this null?
null?
null? length 2.5
b2
boxes null
b1 null
b2 b3
null width 2.2
Yes,
No,
Yes,
No,
No, Assign
Next
Assign
Next
Yes,
Next Index
Index
Assign
Index 0 1 2 height 2.3
length 3.5
Lets assign the object referred by b1, by following these steps b3
Lets assign the object referred by b2, by following these steps width 3.2
Lets assign the object referred by b3, by following these steps height 3.3
Array
Manipulating an Array of Objects
Lets Assume that we have an array of Box class and 3 objects length 1.5
of Box is already assigned in the array. b1
width 1.2
height 1.3
boxes b1 b2 b3 length 2.5
b2
width 2.2
We want to remove the object b2 from the array. How?
height 2.3
Solution: We can only assign these objects in the array if there
is b2 in any of the indexes. The followings steps can be length 3.5
followed to remove an object from the array: b3
• Start from the 1st index of the array and check the value of width 3.2
that index. height 3.3
• If it is b2, assign the null and exit. Else, go to the next index.
• Repeat until the last index.
Array
Manipulating an Array of Objects
• Start from the 1st index of the array and check the value length 1.5
of that index. b1
• If it is b2, assign the null and exit. Else, go to the next width 1.2
index. height 1.3
• Repeat until the last index.
length 2.5
b2
Is it b2 ? width 2.2
boxes b1 null
b2 b3 height 2.3
Yes,No,
Assign
Next null
Index length 3.5
b3
width 3.2
height 3.3
Array
Manipulating an Array of Objects
Lets Assume that we want to print the data of the boxes length 1.5
stored inside the array. How? b1
width 1.2
height 1.3
• Start from the 1st index of the array and check the length 1.5
value of that index. b1
• If it is not null, print data and go to next index. Else, width 1.2
ignore and go to next index. height 1.3
• Repeat until the last index.
length 2.5
Is it not null ? b2
width 2.2
boxes b1 null b3 height 2.3
Yes, Print data and next
Yes, Print data and next
No, Ignore and next length 3.5
b3
width 3.2
height 3.3
Books