----------Arrays-----------
Arrays are objects in Java that store
multiple variables of the same type.
●Arrays can hold either primitives or object
references, but the array itself will always be an object on the heap, even if
the array is declared to hold primitive elements.
-----Array Declaration-----
• Arrays are declared by
stating the type of element the array will hold, which can be an object or a
primitive, followed by square brackets to the left or right of the identifier.
Declaring an Array of
Primitives
•int key [ ]; // Square
brackets after name (legal but less readable)
•int[ ] key; // Square
brackets before name (recommended)
---Array Construction--
Constructing an array means creating the
array object on the heap—in other words, doing a new on the array type.
●To create an array object, Java needs to
know how much space to allocate on the heap, so you must specify the size of
the array at construction time.
Constructing One-Dimensional Arrays
int[] testScores; // Declares the
array of integer
testScores = new int[4]; //constructs an
array and assigns it the testScores variable
Memory Representation
The preceding code puts a new object on the heap—an array
object holding four elements—with each element containing an int with the default value 0.
Always Remember
Arrays must always be given a size at the time they are constructed.
The JVM needs the size to allocate the appropriate space on the
heap for the new array object.
Ex:-
int[] carList = new int[]; // will not compile; needs a
size
int[] x = new int[5];
x[4] = 2; // OK, the last element is at index 4
x[5] = 3;
What
Happen when we tries to access an out of range array index?
• if an array has
five elements, trying to access the [5] element will raise an
ArrayIndexOutOfBoundsException, because in an array of five elements, the legal
index values are 0, 1, 2, 3, and 4.
ArrayIndexOutOfBoundsException, because in an array of five elements, the legal
index values are 0, 1, 2, 3, and 4.
• You also might
see an attempt to use a negative number as an array index.
int []
z = new int[2];
int y
= -3;
z[y] = 4; // Runtime exception.; y is a negative
number
Multi-dimensional Arrays--------
Multidimensional arrays, are simply arrays
of arrays.
A two-dimensional array of type int is really an object of type int array (int []), with each element in that array holding a reference to
another int array.
The second dimension holds the actual int primitives.
To declare a multidimensional array variable,specify each additional index
using another set of square brackets.
Multi-dimensional Array Declaration------
•n-dimensional Arrays
are declared by stating the type of element the array will hold, which can be
an object or a primitive, followed by ‘n’ square brackets to the left or right
of the identifier.
Declaring a 2-dim Array of Primitives
•
int myArray [ ][ ]; // Square brackets after name (legal but less
readable)
int[ ][ ] myArray; // Square brackets before name (recommended)
Declaration:-
The following declares a two-dimensional array variable called myArray.
int myArray[][] = new int[3][3];
Initialization:-
int [ ] a={5,6,7};
myArray[0]={1,2,3};
myArray[1]=new int[ ]{3,4,5};
myArray[2]=a;
myArray[0][0] = 3;
myArray[0][1] = 2;
myArray[0][2] = 1;
Memory representation
of a 2-D array:------
Jagged Array / ZigZag Array:-
Declaration :
int [ ][ ] myArray ;
Construction :
myArray = new int[3][ ];
Note :-
Only the first brackets are given a size. That’s acceptable in
Java, since the JVM needs to know only the size of the object assigned to the
variable.
Initialization :
int [ ] a=new int[]{9,8,5};
myArray[0] = new int[]{6,7};
myArray[1] =a;
Memory Representation
of a Jagged Array:-
:
Declaring, Constructing, and Initializing:-
Declaring, Constructing, and Initializing:-
Two different array-specific syntax shortcuts to both
initialize and construct in a single statement.
To declare, create, and initialize in one statement as follows:
int x =
9;
int [ ] myArray = {3,6,x,8};
Shortcut syntax with jagged arrays:
Ex:-
int[ ][ ] scores = {{5,2,4,7}, {9,2}, {3,4}};
scores[0] // an array of four ints
scores[1] // an array of 2 ints
scores[2] // an array of 2 ints
scores[0][1] // the int value 2
scores[2][1] // the int value 4
great progms!!
ReplyDeletethanx sreelekshmy
Delete