Lecture 3 Arrays on object oriented programming language
1. Course: Object Oriented Programming (CS F213)
20/01/25
Prof. Anita Agrawal,
BITS-Pilani-K.K. Birla Goa campus
LECTURE 3A
ARRAYS
1
2. ARRAYS
❖An array holds a fixed number of values of a single
type.
❖It allows to store and access large number of values
conveniently.
❖Syntax:
datatype[] identifier;
❖ Example: int [ ] age;
❖ Alternate form: int age [];
2
Anita Agrawal (CS F213)
3. ❖ dataType can be a primitive data type like: int, char, byte
etc. or an object.
❖ Identifier (variable name) can be anything provided that it
follows the rules and conventions of identifiers.
❖ int [ ] age; [value of age is null]
does not actually create an array;
3
Anita Agrawal (CS F213)
4. CREATING THE ARRAY
To create an array, use the new operator.
Syntax: var-name = new type[size];
▪ Example: age = new int [5];
allocates memory for 5 integer elements and assigns the array
to the age variable.
▪ Default value: 0 for numeric
▪ false for boolean, etc..
4
Anita Agrawal (CS F213)
5. ALTERNATE WAY
int [] age;
age = new int [5]; int[] age = new int[5];
Imp: Once the length of the array is defined, it cannot
be changed in the program.
5
Anita Agrawal (CS F213)
6. ACCESSING THE ARRAY
❖In case of primitive data types, the actual values
are stored in contiguous memory locations.
❖Each element is accessed by its numerical index.
❖Numbering begins with 0.
❖The 5th element, for example, would therefore be accessed at index 4.
6
Anita Agrawal (CS F213)
7. INITIALISING THE ARRAY
int[] age = new int[5];
age [0] = 10; // initialize first element
age [1] = 5; // initialize second element
age [2] = 30; // and so forth
7
Anita Agrawal (CS F213)
8. ALTERNATE SYNTAX
Shortcut syntax to create and initialize an array:
▪ int[ ] age = { 10, 5, 30, 25,40 };
Here the length of the array is determined by the number of
values provided between braces and separated by commas.
8
Anita Agrawal (CS F213)
9. EXAMPLE
Write a main method to initialise an array ‘age’
with the values 1,5,2,7,8 and display the values
on the console in the form:
Element at index 0: 1
:
:
:
.
9
ArrayExample.java
Anita Agrawal (CS F213)
10. OUTPUT
Element at index 0: 1
Element at index 1: 5
Element at index 2: 2
Element at index 3: 7
Element at index 4: 8
10
Anita Agrawal (CS F213)
11. EXAMPLE 2
Write a main method to declare an array ‘age’of
5 elements and initialise the values of the 1st
and 3rd element with 34 and 14 respectively.
Display the values of all the elements on the
console in the form:
Element at index 0: 1
11
ArrayExample.java
Anita Agrawal (CS F213)
12. OUTPUT OF THE PROGRAM
Element at index 0: 34
Element at index 1: 0
Element at index 2: 14
Element at index 3: 0
Element at index 4: 0
12
Anita Agrawal (CS F213)
13. MULTIDIMENSIONAL ARRAYS
An array of arrays/multidimensional array.
An array whose components are themselves arrays, unlike
C or Fortran.
The rows are allowed to vary in length.
13
Anita Agrawal (CS F213)
17. MULTIDIMENSIONAL ARRAYS (CONTD…)
For a multidimensional array, we only need to specify the
size for the first (leftmost) dimension
int [][] twoD = new int[4][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[3];
twoD[3] = new int[4];
Also possible to write: twoD[0]={10,6,12,13,5};
17
Anita Agrawal (CS F213)
18. MULTIDIMENSIONAL ARRAYS (CONTD…)
No need to allocate the same number of elements
for each dimension
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
18
twoD.java
Anita Agrawal (CS F213)