KEMBAR78
Lecture 3 Arrays | PDF | Integer (Computer Science) | Data Type
0% found this document useful (0 votes)
17 views18 pages

Lecture 3 Arrays

Uploaded by

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

Lecture 3 Arrays

Uploaded by

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

LECTURE 3A

ARRAYS

Course: Object Oriented Programming (CS F213)


25/08/25

Prof. Anita Agrawal,


BITS-Pilani -K.K. Bir la Goa campus

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 1


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

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 2


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

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 3


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..

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 4


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.

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 5


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.

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 6


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

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 7


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/25/2025 5:32 PM Anita Agrawal (ECOM F213) 8


EXAMPLE

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
:
:
:
.

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) ArrayExample.java 9


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

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 10


EXAMPLE 2

Declare an array ‘age’of 5 elements and


initialise the values of the 1st and 3 rd element
with 34 and 14 respectively. Display the values
of all the elements on the console in the form:
Element at index 0: 1

8/25/2025 5:32 PM Anita Agrawal (ECOM F213)


ArrayExample.java
11
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

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 12


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.

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 13


Right index:
column

Left index:
row

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 14


MULTIDIMENSIONAL ARRAYS (CONTD…)

Example:

String[][] names = { {"Mr. ", "Mrs. ", "Ms. "},

{“X", “Y"} }

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 15


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

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 16


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

8/25/2025 5:32 PM Anita Agrawal (ECOM F213)


twoD.java 17
ALTERNATE DECLARATION

int[] a1, a2, a3;


or
int a1[], a2[], a3[];

Next……classes and objects

8/25/2025 5:32 PM Anita Agrawal (ECOM F213) 18

You might also like