[go: up one dir, main page]

0% found this document useful (0 votes)
14 views26 pages

Lecture 4.1 - Array new

The document provides an overview of arrays in Java, including their declaration, initialization, and manipulation. It covers both one-dimensional and two-dimensional arrays, as well as arrays of objects, detailing various approaches for creating and accessing them. Key concepts such as memory representation, default values, and methods for printing and modifying array elements are also discussed.

Uploaded by

abhasan7710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views26 pages

Lecture 4.1 - Array new

The document provides an overview of arrays in Java, including their declaration, initialization, and manipulation. It covers both one-dimensional and two-dimensional arrays, as well as arrays of objects, detailing various approaches for creating and accessing them. Key concepts such as memory representation, default values, and methods for printing and modifying array elements are also discussed.

Uploaded by

abhasan7710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Array

Course Code: CSC1205 Course Title: Object Oriented Programming 1 (JAVA)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: Week No: Semester:


Lecturer: Name & email
Lecture Outline

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.

int age = 22; age 22

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.

The solution is Array


An array is a collection of similar type of data or value. So, an array of integer type
will only hold integer values and an array of double type will only hold double values.
There are 4 approaches to declare an 1D array. We will illustrate the memory
representations and discuss the value initializations process one by one.
Array
Declaring 1D Array : 1st Approach

1st Approach Here,


int arr1[ ] = new int [5]; • int is the type of array.
Or, • arr1 is the name of the array.
int [ ]arr1 = new int [5]; • The [ ] symbol denotes that it is an array, not a variable.
• new keyword allocates memory for the array.
• int [5] denotes that memory need to be allocated for 5
integer values.

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];

According to the concept of default values, all


the index positions will be initialized as 0.
arr1 11
0 0 0 0
18 0
Now, if we want to initialize a value in a
0 1 2 3 4
particular index, we can do it like this:

Index Positions arr1 [0] = 11;


arr1 [3] = 18;
Similarly, we can also access the elements of the array using their index positions.
Array
Declaring 1D Array : 2nd Approach

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

2nd Approach size = 18 / 6 = 3

int arr2[ ]; arr2 0


11 0 0
15
int size = arr1[3] / 6;
0 1 2
arr2 = new int [size];

According to the concept of default values, all


the index positions will be initialized as 0.

Now, if we want to initialize a value in a particular index, we can do it like this:

arr2 [0] = 11;


arr2 [2] = 15;
Similarly, we can also access the elements of the array using their index positions.
Array
Declaring and Initializing 1D Array : 3rd Approach

3rd Approach • This approach is used when we do not


know the size of the array but we know the
int arr3[ ] = new int [ ] {11,22,33,44}; elements of the array.
Or, • The size gets computed automatically from
int [ ]arr3 = new int [ ] {11,22,33,44}; the number of elements of the array.
Here,
• int is the type of array.
• arr3 is the name of the array.
• The [ ] symbol denotes that it is an array, not a variable.
• new keyword allocates memory for the array.
• int [ ] {11,22,33,44} denotes that memory will be allocated for four integers (as
there are four integers inside the curly braces) and the value 11, 22, 33 and 44 will
be initialized in the array.
Array
Memory Representation of an Array: 3rd Approach

3rd Approach
arr3 11 22 33
15 44
int arr3[ ] = new int [ ] {11,22,33,44}; 0 1 2 3

If we want to change a value in a particular index, we can do it like this:

arr3 [2] = 15;


Similarly, we can also access the elements of the array using their index positions.
Array
Declaring and Initializing 1D Array : 4th Approach

4th Approach

int arr4[ ] = {11,22,33,44,55}; This approach is NOT RECOMMENDED.


Or,
int [ ]arr4 = {11,22,33,44,55};
Here,
• int is the type of array.
• arr4 is the name of the array.
• The [ ] symbol denotes that it is an array, not a variable.
• Memory will be allocated for five integers (as there are five integers inside the curly
braces) and the value 11, 22, 33, 44 and 55 will be initialized in the array.

arr4 11 22 15 44 55
0 1 2 3 4
Array
Printing array elements

We can simply print all the elements of an arr 11 22 33 44 55


array using a loop. Lets assume that we have
0 1 2 3 4
the following array:
int arr[ ] = new int [ ] {11,22,33,44,55};

for(int i=0; i<arr.length;


i<5; i++){ i++){ int i=0;
System.out.println(arr[i]); do{
} System.out.println(arr[i]);
i++;
int i=0; }
while(i<5){
while(i<arr.length){ while(i<5);
while(i<arr.length);
System.out.println(arr[i]);
i++; The length attribute of any array
} represents the size of the array.
Array
Declaring 2D Array

1st Approach: int arr5[ ][ ] = new int [3][3]; Here,

2nd Approach: • The [ ][ ] symbol denotes


that it is a 2D array.
int arr6[ ][ ]; • The [ ][ ] symbols can also
be written before the
row = arr1[3]/9;
name of the array.
col = arr1[3]/6; • The value in the 1st [ ]
arr6 = new int [row][col]; represents the number of
rows and the value in 2nd [
3rd Approach: int arr7 [ ][ ] = new int [ ][ ]{{1,2}, {3,4}}; ] represents the number of
columns.
4th Approach: int arr8[ ][ ] = {{7,8},{9,5}};

The 4th approach is NOT RECOMMENDED.


Array
Memory Representation of 2D Array

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:

int arr6[ ][ ]; Column index


row = arr1[3]/9;
col = arr1[3]/6; 0 1 2

Row index
arr6 = new int [row][col]; 0 11
0 0 0

As, no elements has been initialized, all the index 1 0 0 18


0
positions will be initialized by the default value 0. arr6

Now, if we want to initialize a value in a particular index,


we can do it like this:

arr6 [0][0] = 11;


arr6 [1][2] = 18;
Array
Memory Representation of 2D Array

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:

arr7 [0][0] = 11;


arr7 [1][1] = 18;
Array
Array of Objects

The following statement creates an object of the Box length 0.0


class we studied previously:
b width 0.0
Box b = new Box( ); height 0.0
This b can store data for only one Box object.
How can we store data for multiple boxes?
Solution: An array of Box.
Box boxes[ ] = new Box [3];
boxes null null null
As, no objects has been initialized in the boxes array, 0 1 2
the default value for each of the indexes will be null.
We can create object of Box class referring the index of the boxes array
or we can create object of Box class and assign them in the boxes array.
Array
Array of Objects

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

Box b1 = new Box(1.5,1.2,1.3); height 1.3


Box boxes[ ] = new Box [3];
boxes b1
null null null
We want to assign the b1 object in the boxes[0] position. 0 1 2
boxes[0] = b1; Output
So, now, the following two statements will yield the
same output: 1.5
1.5
System.out.println(b1.getLength( ));
System.out.println(boxes[0].getLength( )); WHY???
Array
Array of Objects

length 1.5 length 2.7


boxes b1 null null
b1 width 1.2 width 2.5
0 1 2
height 1.3 height 1.2

Memory location of boxes[0] holds the reference b1. Output


So, they both refers to the same object.
We can also create an object and refer that object directly by 2.7
using an index of the array.

boxes[1] = new Box(2.7,2.5,1.2);


Now, we can use boxes[1] to access that object. For Example:
System.out.println(boxes[1].getLength( ));
Array
Manipulating an Array of Objects

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

boxes b1 null b3 length 2.5


b2
width 2.2
Solution: We can not print data from an index if there is height 2.3
null in that index. The followings steps can be followed
to print data of the boxes stored in the array: length 3.5
• Start from the 1st index of the array and check the b3
width 3.2
value of that index.
• If it is not null, print data and go to next index. Else, height 3.3
ignore and go to next index.
• Repeat until the last index.
Array
Manipulating an Array of Objects

• 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

• Java Complete Reference, 7th Edition, By Herbert Schildt.


• A Programmer’s Guide to Java™ SCJP Certification A Comprehensive Primer,
3rd edition, by Khalid A. Mughal and Rolf W. Rasmussen
• Java How to Program Java, 9th Edition, By Deitel and Deitel.
• The Java Language Specification, By J. Gosling, B. Joy, G. Steele, G.Bracha and
A. Buckley
• Introduction to Programming Using Java, 6th Edition, By David j. Eck
• Head First Java, By Kathy Sierra and Bert Bates
References

• Java Complete Reference, 7th Edition, By Herbert Schildt.


• A Programmer’s Guide to Java™ SCJP Certification A Comprehensive Primer,
3rd edition, by Khalid A. Mughal and Rolf W. Rasmussen
• The Java Language Specification, By J. Gosling, B. Joy, G. Steele, G.Bracha and
A. Buckley
• docs.oracle.com

You might also like