0% found this document useful (0 votes)
4 views11 pages

Lesson 1 Introduction to Array

This document provides an introduction to arrays in computer programming, explaining that arrays allow for the storage of multiple items of the same data type in contiguous memory. It covers the declaration, instantiation, and initialization of arrays, along with examples of how to reference and manipulate array elements. The document also includes sample program statements demonstrating the use of arrays compared to single variables.

Uploaded by

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

Lesson 1 Introduction to Array

This document provides an introduction to arrays in computer programming, explaining that arrays allow for the storage of multiple items of the same data type in contiguous memory. It covers the declaration, instantiation, and initialization of arrays, along with examples of how to reference and manipulate array elements. The document also includes sample program statements demonstrating the use of arrays compared to single variables.

Uploaded by

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

UNIT 2

Arrays

CC 104 Computer Programming 2


LESSON

1 Introduction to Array

CC 104 Computer Programming 2


An array allows you to store a group of
What is items of the same data type together in
memory.
an Array
The contents of an array are stored in
? contiguous memory.
It is a special kind of object but it is often
more useful to think as a collection of
variables of the same type.
LESSON 1
CC 104 Computer Programming 2
The data items in an array are referred
Array to as elements.

Elements Each element is assigned a unique


number known as subscript or index.
and It can be individually referenced by
Subscript specifying the index inside the square
s brackets [ ].

LESSON 1
CC 104 Computer Programming 2
Array num [0] [1] [2] [3] [4]

Elements // num is an array variable of five elements

and Each element can be referred to as :

Subscript num[0] num[1] num[2]


s num[3] num[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];

// n is the size or length of the array


LESSON 1
CC 104 Computer Programming 2
Example:

int[] num = new int[5];


Declaratio
n of an
Array
Note: In a fixed-length array, the size of the array is
constant and must have a value at compilation time.

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

char[] choice ={‘a’,’b’,’c’,’d’,’e’};

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 ;

s using a = a + 1 ; a[1] = a[1] + 1 ;

Array c = a + b ; c = a[1] + b[2] ;

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

You might also like