Array:: Tuesday, July 21, 2020 1:43 PM
Array:: Tuesday, July 21, 2020 1:43 PM
Array :
Notes :
1. Array is fixed in its size, ( we cannot increase or reduce the size of an array
during runtime )
2. In array we can store multiple values, but it should be of same type.
( Homogeneous )
3. We can access the elements present in an array with the help of an integer
number starting from 0 up to the size of the array - 1, it is known as index or
subscript of an array. ( example if the size of the array is 5, then the index start
from 0 and ends at 4 i.e., index : 0, 1, 2, 3, 4 )
We can create an array reference variable with the help of array operator [ ]
and the datatype.
Syntax :
datatype[] identifier ;
datatype identifier[] ;
Arrays Page 1
ex :
=========================================================
Syntax:
Arrays Page 2
For example refer:
Note :
in this syntax, the array length is same as the number of elements passed in
the initialization.
syntax :
new datatype[ size ]
Note :
1. new returns reference of array object created.
2. the array object created will be assigned with the default value of
its type
Arrays Page 3
To Access the elements of an Array :
Syntax :
array_reference [ index ]
Arrays Page 4
Note :
If array index is less than zero or, greater than or equal to length of the array
we get AIOOBE.
Assignment1 :
1. WAJP to read names of 5 cities from the user, store them in the array, and
display the city names along with their length.
++++++++++++++++++++++++++++++++++++++++++++++++++++++
Arrays Page 5
1. A method can return the array reference to the caller.
2. To achieve this design the return type of the method must be an array type.
=====================================================
Note :
In java we have a built in class called as Arrays in util package. there the fully
qualified name is java.util.Arrays, which has lot of built in methods to perform
array operations such as :
copy array, sort array etc…
Task2 :
1. ask the user total number of best friends he has got in his life time
2. read the names of all the best friends and store them in the array
3. display his best friend names in the ascending order.
Arrays Page 6
Assignment 3 :
Attributes :
1. company name
2. an array of size 5 to store Employees
Arrays Page 7
Pg2
Monday, May 18, 2020 9:52 AM
Note:
1. sort method can sort the array only if all the objects present in the array
is comparable type.
2. If the object is not comparable type we get ClassCastException
Note :
1. We can sort a non-primitive array, using sort() method, but the objects
stored in the array must be made comparable type.
Note:
1. Comparable interface has only one abstract method called
compareTo().
Method declaration is :
Arrays Page 8
1. compareTo method is used to compare 2 objects.
2. it can accept only one object as argument.
3. compareTo method compares the current object with the object passed as
argument.
4. return type of compareTo method is int, Therefore
Question:
Task 1 :
ST1 :
1. Design a blueprint for Laptop, make the class comparable type.
2. properties of latop( ram_size , hard_disk, retail_price )
3. create suitable constructors
4. override toString, hashCode and equals method.
ST2 :
Arrays Page 10