Lec - 04 - Arrays
Lec - 04 - Arrays
Programming
Topics To Be Covered Today
Array
◦ Single & Multi-dimensional
Arrays
An array is a group of liked-typed variables
referred to by a common name, with individual
variables accessed by their index.
Normally, array is a collection of similar type of
Arrays are:
1) Declared
2) Created (instantiated)
3) Initialized
4) Used
Array Declaration
Array declaration involves:
1) declaring an array identifier
2) declaring the number of dimensions
3) declaring the data type of the array elements
Three styles of array declaration:
type array-variable[ ];
e.g. int abc[ ];
or
type [ ]array-variable; Datatype => int
Identifier => abc
or Dimention=> single
type[ ] array-variable;
Array Creation
After declaration, no array actually exists.
In order to create an array, we use the new
operator:
type array-variable[ ];
array-variable = new type[size];
This creates a new array to hold size elements of
Comments:
1) there is no need to use the new operator
2) the array is created large enough to hold all specified
elements
Example: Array Initialization
Passing Array to method in java
Multi-dimensional Array
Multidimensional arrays are arrays of arrays:
1) declaration
int array[ ][ ];
2) creation
int array[ ][ ] = new int[2][3];
3) initialization
int array[ ][ ] = { {1, 2, 3}, {4, 5, 6} };
Example: Multi-dimensional Array
class name of java array?
In java, array is an object.
For array object, a proxy class is created whose
arraycopy(Object src, int srcPos,Object dest, int destPos,
int length)
Example
for(int i=0;i<copyTo.length;i++)
System.out.println(copyTo[i]);
Pros and Cons of Arrays
Advantages Disadvantages
Code Optimization Size Limit