Prepared by-
Shaishav shah(170120116094)
Guided by – Prof. Nisha Patel
Gandhinagar Institute of Technology
SUBJECT - OOPJ (2150704)
Arrays in Java
WHATISARRAY?
Java provides a data structure, the array, which
stores a fixed-size sequential collection of
elements of the same type. An array is used to
store a collection of data, but it is often more
useful to think of an array as a collection of
variables of the same type.
ARRAYS
• DECLARA
TION
• CONSTRUCTION
• INITIALIZATION
• TWODIMENSIONALARRA
Y
• MULTIDIMENSIONALARRA
Y
WHYARRAY?
Problem?
•Implement an application that will calculate 100
students exam average.
•Variables needed?
int studentA; int studentB; int studentC; int studentD;
...
SEVERALVARIABLESATONCE?
Arraycomesto the rescue!
• Justalist of variables
• Declarethe array
int [] array;
• Initialize the array and set it'ssize
array =new array[3];
• Storevaluesinto array
array[0] =2;
array[1] =3;
array[2] =7;
DECLARATION
• Declaring
Arrays:
Int[] mark; Byte[]
age; Double[]
height;
Int mark[]; Byte
age[];
Double height[];
Data type
Array
Name
worksbut not preferredway
preferredway
DECLARATIONCOUNT…
• Array declaration in C++
int hardy[10];
• Array declaration in Java
int [] hardy;
hardy=new int[10];
DECLARATIONCOUNT…
• Storage for the array itself is not allocated
until you use “new”.
• For initializing method the “new” command is
not needed.
int [] hardy={5,3,7,89,2};
CONSTRUCTION
Int [] hardy;
hardy=new int[5];
In single line
Int[] hardy=new
int[5];
Hardyarray
INITIALIZATION
• Initialization isloading the array withthe valies.
Int[] hardy=newint[5]
hardy[0]=32
;
hardy[1]=12
;
hardy[2]=66
;
32 12 66 54 43
0 1 2 3 4
index
TWODIMENSIONALARRAY
• Int[][] hardy=new
int[2][3]; hardy[0][0]=32;
hardy[0][1]=12;
hardy[0][2]=66;
hardy[1][0]=54;
hardy[1][1]=43;
hardy[1][2]=36;
32 12 66 54 43 36
0 1
0 1 2 0 1 2
TWO DIMENSIONAL ARRAY -
NON UNIFORM
• Int[][] hardy=newint[2];
• Mark[0]=newint[3];
• Mark[1]=newint[4];
0 1
0 1 2 0 1 2 3
MULTIDIMENSIONALARRAY
• Int[][][] hardy=newint[2][3][2];
0 1
0 1 2 0 1 2
0 1 0 1 0 1 0 1 0 1 0 1
Arrays in java oopj

Arrays in java oopj