Arrays

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Arrays in Java

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;

• datatype arrayRefVar[]; // This style is allowed, but not


preferred
Example:
double myList[]; 5
More declaration
One dimensional array declaration:
Array can be declared in three ways:
int[] x;
int []x;
int x[];
The array size can’t specified yet the time of array
declaration
double[] myList = new double[10];

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];

myList[0] references the first element in the array.


myList[9] references the last element in the array.
8
Declaring and Creating
in One Step

• datatype[] arrayRefVar = new


datatype[arraySize];
double[] myList = new double[10];

• datatype arrayRefVar[] = new


datatype[arraySize];
double myList[] = new double[10];
9
Array Initializers

• Declaring, creating, initializing in one step:


double[] myList = {1.9, 2.9, 3.4, 3.5};

This shorthand syntax must be in one


statement.

10
Declaring, creating, initializing Using the Shorthand Notation

double[] myList = {1.9, 2.9, 3.4, 3.5};

This shorthand notation is equivalent to the following


statements:
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
11
Array declaration
Two dimensional array declaration:
Array can be declared in six ways:
int[][] x; int[] []x;
int [][]x; int[] x[];
int x[][]; int []x[]
The above all are valid declaration
Two Dimensional Arrays
Item1 Item2 Item3

• Two dimensional arrays Sold


Person
allows us to store data Salesgirl #1 10 15 30

that are recorded in


table. For example: Salesgirl #2 14 30 33

• Table contains 12 items, Salesgirl #3 200 32 1

we can think of this as a


matrix consisting of 4 Salesgirl #4 10 200 4

rows and 3 columns.


13
2D arrays manipulations
• Declaration:
– int myArray [][];
• Creation:
– myArray = new int[4][3]; // OR
– int myArray [][] = new int[4][3];
• Initialisation:
– Single Value;
• myArray[0][0] = 10;
– Multiple values:
• int tableA[2][3] = {{10, 15, 30}, {14, 30, 33}};
• int tableA[][] = {{10, 15, 30}, {14, 30, 33}};
14
Two-Dimensional Arrays
• Declaring a two-dimensional array requires two sets of brackets
and two size declarators
– The first one is for the number of rows
– The second one is for the number of columns.
double[][] scores = new double[3][4];
two dimensional array rows columns

• 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

The scores variable


holds the address of a
2D array of doubles. column 0 column 1 column 2 column 3
Address
row 0 scores[0][0] scores[0][1] scores[0][2] scores[0][3]

row 1 scores[1][0] scores[1][1] scores[1][2] scores[1][3]


scores[2][0] scores[2][1] scores[2][2] scores[2][3]
row 2
Accessing Two-Dimensional Array Elements
Accessing one of the elements in a two-dimensional
array requires the use of both subscripts.
The scores variable
scores[2][1] = 95;
holds the address of a
2D array of doubles.
column 0 column 1 column 2 column 3
Address
row 0 0 0 0 0
row 1 0 0 0 0
row 2 0 95 0 0
Accessing Two-Dimensional Array Elements
• When processing the data in a two-
dimensional array, each element has two
subscripts:
– one for its row and
– another for its column.
Thank you

You might also like