PROGRAMMING IN JAVA
Arzath Areeff
WHAT IS ARRAY?
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
• DECLARATION
• CONSTRUCTION
• INITIALIZATION
• TWO DIMENSIONAL ARRAY
• MULTI DIMENSIONAL ARRAY
WHY ARRAY?
Problem?
• Implement an application that will calculate 100
students exam average.
• Variables needed?
int studentA;
int studentB;
int studentC;
int studentD;
...
SEVERAL VARIABLES AT ONCE?
Array comes to the rescue!
• Just a list of variables
• Declare the array
int [] array;
• Initialize the array and set it's size
array = new array[3];
• Store values into 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
works but not preferred way
preferred way
DECLARATION COUNT…
• Array declaration in C++
int hardy[10];
• Array declaration in Java
int [] hardy;
hardy =new int[10];
DECLARATION COUNT…
• 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];
Hardy array
INITIALIZATION
• Initialization is loading the array with the valies.
Int[] hardy=new int[5]
hardy[0]=32;
hardy[1]=12;
hardy[2]=66;
hardy[3]=54;
hardy[4]=43;
32 12 66 54 43
0 1 2 3 4
index
TWO DIMENSIONAL ARRAY
• 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=new int[2];
• Mark[0]=new int[3];
• Mark[1]=new int[4];
0 1
0 1 2 0 1 2 3
MULTI DIMENSIONAL ARRAY
• Int[][][] hardy=new int[2][3][2];
0 1
0 1 2 0 1 2
0 1 0 1 0 1 0 1 0 1 0 1
Peace & Love

Arrays in java

  • 1.
  • 2.
    WHAT IS ARRAY? Javaprovides 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.
  • 3.
    ARRAYS • DECLARATION • CONSTRUCTION •INITIALIZATION • TWO DIMENSIONAL ARRAY • MULTI DIMENSIONAL ARRAY
  • 4.
    WHY ARRAY? Problem? • Implementan application that will calculate 100 students exam average. • Variables needed? int studentA; int studentB; int studentC; int studentD; ...
  • 5.
    SEVERAL VARIABLES ATONCE? Array comes to the rescue! • Just a list of variables • Declare the array int [] array; • Initialize the array and set it's size array = new array[3]; • Store values into array array[0] = 2; array[1] = 3; array[2] = 7;
  • 6.
    DECLARATION • Declaring Arrays: Int[]mark; Byte[] age; Double[] height; Int mark[]; Byte age[]; Double height[]; Data type Array Name works but not preferred way preferred way
  • 7.
    DECLARATION COUNT… • Arraydeclaration in C++ int hardy[10]; • Array declaration in Java int [] hardy; hardy =new int[10];
  • 8.
    DECLARATION COUNT… • Storagefor 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};
  • 9.
    CONSTRUCTION Int [] hardy; hardy=newint[5]; In single line Int[] hardy=new int[5]; Hardy array
  • 10.
    INITIALIZATION • Initialization isloading the array with the valies. Int[] hardy=new int[5] hardy[0]=32; hardy[1]=12; hardy[2]=66; hardy[3]=54; hardy[4]=43; 32 12 66 54 43 0 1 2 3 4 index
  • 11.
    TWO DIMENSIONAL ARRAY •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
  • 12.
    TWO DIMENSIONAL ARRAY- NON UNIFORM • Int[][] hardy=new int[2]; • Mark[0]=new int[3]; • Mark[1]=new int[4]; 0 1 0 1 2 0 1 2 3
  • 13.
    MULTI DIMENSIONAL ARRAY •Int[][][] hardy=new int[2][3][2]; 0 1 0 1 2 0 1 2 0 1 0 1 0 1 0 1 0 1 0 1
  • 14.