You are on page 1of 17

LESSON 2:

ARRAY
DCIT 50 – Object Oriented Programming
Prepared by: Marie Joy Ignacio
▪An array is a
container object
that holds a fixed
number of values
of a single type.
•Code Optimization: It
makes the code
optimized, we can
ADVANTAG retrieve or sort the data
efficiently.
ES:
•Random access: We can
get any data located at an
index position.
▪ Size Limit:  We can store only
the fixed size of elements in the
DISADVAN
array. TAGE:
HOW TO DECLARE ARRAY IN
JAVA
Data Type

Array Name
HOW MANY ELEMENTS CAN
ARRAY THIS HOLD?
▪ For example: ▪ Example of an array in single statement
HOW TO ACCESS ELEMENTS
OF AN ARRAY IN JAVA?
EXAMPL
E: ARRAY
ACCESS
ELEMEN
TS
JAVA ARRAY INITIALIZATION

age[0] age[1] age[2] age[3] age[4]

12 4 5 2 5
OUTPUT:
LOOPING THROUGH ARRAY
ELEMENTS
▪ Example: Using for Loop
OUTPUT:
class Testarray{  
public static void main(String args[]){  
int a[]=new int[5];
  a[0]=10;  
a[1]=20;  

EXAMPLE a[2]=70;  
a[3]=40;  
2: a[4]=50;  

for(int i=0; i <a.length; i++)


System.out.println (a[i]);  
}
}  
10
20
OUTPUT:
70
40
50
REFERENCES:
▪ https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
▪ https://www.javatpoint.com/array-in-java
THANK YOU !

You might also like