SlideShare a Scribd company logo
Course: Object Oriented Programming (CS F213)
20/01/25
Prof. Anita Agrawal,
BITS-Pilani-K.K. Birla Goa campus
LECTURE 3A
ARRAYS
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 [];
2
Anita Agrawal (CS F213)
❖ 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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
Right index:
column
Left index:
row
Anita Agrawal (CS F213) 14
MULTIDIMENSIONAL ARRAYS (CONTD…)
Example:
String[][] names = { {"Mr. ", "Mrs. ", "Ms. "},
{“X", “Y"} }
15
Anita Agrawal (CS F213)
MULTIDIMENSIONAL ARRAYS (CONTD…)
Print:
Mr. X
Ms. Y
16
Anita Agrawal (CS F213)
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)
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)
ALTERNATE DECLARATION
int[] a1, a2, a3;
or
int a1[], a2[], a3[];
Next……classes and objects
19
Anita Agrawal (CS F213)

More Related Content

PDF
javaarray
PDF
Java chapter 6 - Arrays -syntax and use
PPTX
Arrays in programming
PPTX
Array lecture
PPTX
6_Array.pptx
PPTX
Chapter6 (4) (1).pptx plog fix down more
PPTX
PPT
Array
javaarray
Java chapter 6 - Arrays -syntax and use
Arrays in programming
Array lecture
6_Array.pptx
Chapter6 (4) (1).pptx plog fix down more
Array

Similar to Lecture 3 Arrays on object oriented programming language (20)

PPT
Comp102 lec 8
PDF
(2) collections algorithms
PDF
Class notes(week 4) on arrays and strings
PPT
ch06.ppt
PPT
PPT
ch06.ppt
PPT
array Details
PPT
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
PPT
Arrays in Java Programming Language slides
PPTX
07+08slide.pptx
DOCX
Class notes(week 4) on arrays and strings
PPT
17-Arrays en java presentación documento
PPTX
Introduction To Practical Computer Science I - Lec 12 (Arrays).pptx
PPTX
Arrays and Strings engineering education
PPTX
Chapter 7.1
PPT
Ap Power Point Chpt6
PPTX
Array
PDF
Week06
PPTX
Java arrays
PPTX
Chap1 array
Comp102 lec 8
(2) collections algorithms
Class notes(week 4) on arrays and strings
ch06.ppt
ch06.ppt
array Details
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
Arrays in Java Programming Language slides
07+08slide.pptx
Class notes(week 4) on arrays and strings
17-Arrays en java presentación documento
Introduction To Practical Computer Science I - Lec 12 (Arrays).pptx
Arrays and Strings engineering education
Chapter 7.1
Ap Power Point Chpt6
Array
Week06
Java arrays
Chap1 array
Ad

Recently uploaded (20)

PDF
Villa Thesis-Final.pdf NNNNNNNNNNNNNNNNNNNNNNNNNNNNN
PPTX
628664134-LS1-English-Figure-of-Speech.pptx
PPTX
Cyber_Awareness_Presrerereerentation.pptx
PDF
Leading the Future of Logistics_ Asib Mahmud’s Vision for Smart Fleets.pdf
PDF
Invincible season 2 storyboard revisions seq3 by Mark G
DOCX
Digital Marketing In Chandigarh Excellence Technology
PDF
Daisia Frank: Strategy-Driven Real Estate with Heart.pdf
PPTX
2200jejejejejjdjeiehwiwheheu1002031.pptx
PPTX
Public_Health_Informghiufdrgatics_PPT.pptx
PDF
PowerPoint Presentation -- Khai Y -- 7891fd01905c67ba9330323ac4f6626e -- Anna...
PPTX
single phase transformer working types and application
PPT
Leadership essentials to build your carrier
PPTX
Interview skill/ //////////////////.pptx
PDF
Looking forward to a challenging Role in the same area and would like to expl...
PPTX
First Homeroom Meeting in Dahlia SY2025-2026
PPTX
LIFE ORIENTATION SLIDES 2025 Grade 11.pptx
PPTX
Jaipur Sees Exponential Growth in Data Analytics Jobs Salarite Smart Hiring P...
PPTX
IDP PPT Format. .pptx
PDF
Invincible Season 2 Storyboard Revisions by Mark G
PDF
Business Valuation: Meaning, Importance & Top Methods
Villa Thesis-Final.pdf NNNNNNNNNNNNNNNNNNNNNNNNNNNNN
628664134-LS1-English-Figure-of-Speech.pptx
Cyber_Awareness_Presrerereerentation.pptx
Leading the Future of Logistics_ Asib Mahmud’s Vision for Smart Fleets.pdf
Invincible season 2 storyboard revisions seq3 by Mark G
Digital Marketing In Chandigarh Excellence Technology
Daisia Frank: Strategy-Driven Real Estate with Heart.pdf
2200jejejejejjdjeiehwiwheheu1002031.pptx
Public_Health_Informghiufdrgatics_PPT.pptx
PowerPoint Presentation -- Khai Y -- 7891fd01905c67ba9330323ac4f6626e -- Anna...
single phase transformer working types and application
Leadership essentials to build your carrier
Interview skill/ //////////////////.pptx
Looking forward to a challenging Role in the same area and would like to expl...
First Homeroom Meeting in Dahlia SY2025-2026
LIFE ORIENTATION SLIDES 2025 Grade 11.pptx
Jaipur Sees Exponential Growth in Data Analytics Jobs Salarite Smart Hiring P...
IDP PPT Format. .pptx
Invincible Season 2 Storyboard Revisions by Mark G
Business Valuation: Meaning, Importance & Top Methods
Ad

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)
  • 15. MULTIDIMENSIONAL ARRAYS (CONTD…) Example: String[][] names = { {"Mr. ", "Mrs. ", "Ms. "}, {“X", “Y"} } 15 Anita Agrawal (CS F213)
  • 16. MULTIDIMENSIONAL ARRAYS (CONTD…) Print: Mr. X Ms. Y 16 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)
  • 19. ALTERNATE DECLARATION int[] a1, a2, a3; or int a1[], a2[], a3[]; Next……classes and objects 19 Anita Agrawal (CS F213)