Arrays
Arrays
Arrays
lectured by
K.Padmavathi
Agenda
• Introduction
• Array declaration
• Array creation
• Array Initialization
• Array declaration, creation and initialization in a
single line
• Length Vs length()
Introduction
What is the need of array?
let us see with an example.
To represent a single integer value. we can use
int x=10;
To represent a two integer value. then
int x=10;
int y=20;
To represent 10000 integer value , declaring 10000 values in this manner is worst
kind of programming practice
In this case arrays are used . Arrays are used to represent huge number of
values in a single variable
Definition of array
Definition
An array is an indexed collection of
fixed number of homogenous
data elements Disadvantage
• Array is fixed in size so
there is no chance of
• To represent huge
Advantage increasing/decreasing
number of values memory requirement.
• Readability of code is
improved
Declaring Array Variables
• datatype[] arrayRefVar;
Example:
double[] myList;
myList reference
myList[0] 5.6
myList[1] 4.5
Array reference myList[2] 3.3
variable
myList[3] 13.2
myList[4] 4
Array element at
myList[5] 34.33 Element value
index 5
myList[6] 34
myList[7] 45.45
myList[8] 99.993
myList[9] 11123
Creating Arrays
Every array in java is an object only, hence we
can create arrays by using new operator
Syntax:
arrayRefVar = new datatype[arraySize];
Example:
myList = new double[10];
10
Declaring, creating, initializing Using the Shorthand Notation
• The two sets of brackets in the data type indicate that the scores
variable will reference a two-dimensional array.
• Notice that each size declarator is enclosed in its own set of
brackets.
Accessing Two-Dimensional Array Elements