Lesson 1 Introduction to Array
Lesson 1 Introduction to Array
Arrays
1 Introduction to Array
LESSON 1
CC 104 Computer Programming 2
Array num [0] [1] [2] [3] [4]
LESSON 1
CC 104 Computer Programming 2
To declare an array:
datatype[] arrayName;
Declaratio
To instantiate an array object:
n of an
arrayName = new dataType[];
Array
or
dataType[] arrayName = new dataType[n];
LESSON 1
CC 104 Computer Programming 2
An array can be initialized when it is declared
,when initializing the array, the value for
their various indexed variables are enclosed
Initializati in braces and separated by commas.
on of an int[] num = {10,20,30,40,50};
Array 0
3
1
4
2
10 20 30 40 50
LESSON 1
CC 104 Computer Programming 2
num[0] = 10 ; choice[0] = ‘a’ ;
Initializati num[1] = 20 ; choice[1] = ‘b’ ;
on of an num[2]
num[3]
=
=
30
40
;
;
choice[2]
choice[3]
=
=
‘c’
‘d’
;
;
Array num[4] = 50 ; choice[4] = ‘e’ ;
LESSON 1
CC 104 Computer Programming 2
single variable array
Sample num = 20 ; num[0] = 20 ;
Program num[x] = 20 ;
Statement num[x-1]= 20 ;
c[0]= a[1]+b[2] ;
LESSON 1
CC 104 Computer Programming 2
single variable array
Sample num=input.nextInt(); num[0]=input.nextInt();
Program
System.out.print(x); System.out.print(x[1]);
Statement
s using if (grade ›=75) if (grade[3] ›=75)
Array { {
System.out.print(“passed”);System.out.print(“passed”);
} }
LESSON 1
CC 104 Computer Programming 2