Elements of arrays can be accessed quickly if the elements are stored in a block of consecutive location. Array can be one dimensional or two dimensional.
For one dimensional array:
Multi-dimensional arrays:
Row major or column major forms
- Row major: a[1,1], a[1,2], a[1,3], a[2,1], a[2,2], a[2,3]
- Column major: a[1,1], a[2,1], a[1, 2], a[2, 2],a[1, 3],a[2,3]
- In raw major form, the address of a[i1, i2] is
- Base+[[i1-low1]*[high2-low2+1]+i2-low2]*width
Translation scheme for array elements
Limit[array, j] returns nj=highj-lowj+1
place: the temporaryor variables
offset: offset from the base, null if not an array reference
The production:
A suitable transition scheme for array elements would be:
Production RuleSemantic ActionS → L := E{if L.offset = null then emit[L.place ':=' E.place]else EMIT [L.place'['L.offset ']' ':=' E.place];
}E → E+E{E.place := newtemp;
EMIT [E.place ':=' E1.place '+' E2.place];
}E → [E]{E.place := E1.place;}E → L{if L.offset = null then E.place = L.place
else {E.place = newtemp;
EMIT [E.place ':=' L.place '[' L.offset ']'];
}
}L → Elist ]{L.place = newtemp; L.offset = newtemp;
EMIT [L.place ':=' c[Elist.array]];
EMIT [L.offset ':=' Elist.place '*' width[Elist.array];
}L → id{L.place = lookup[id.name];
L.offset = null;
}Elist → Elist, E{t := newtemp;
m := Elist1.ndim + 1;
EMIT [t ':=' Elist1.place '*' limit[Elist1.array, m]];
EMIT [t, ':=' t '+' E.place];
Elist.array = Elist1.array;
Elist.place := t;
Elist.ndim := m;
}Elist → id[E{Elist.array := lookup[id.name];
Elist.place := E.place
Elist.ndim := 1;
}
Where:
ndim denotes the number of dimensions.
limit[array, i] function returns the upper limit along with the dimension of array
width[array] returns the number of byte for one element of array.
Although people speak of an "array of objects" or an "array of Strings," in Java this is implemented as an array of references to objects. Here, for example, is an array declaration in Java:
String[] strArray = { "bat", "ant", "dog", "cat", "eel", "ibx", "fox", "gnu", "cow", "pig" };
And here is the array that is created:
Notice that the cells of the array are all the same size and that the cells contain references [pointers] to the actual String objects. Each String object is implemented somewhere in memory. It does not really matter where, because the references in the array lead to the object no matter where it is. [You may wish to review chapter 49 if this is not clear.]
Inspect this statement:
String myPet = strArray[2] ;
This copies the reference in cell number 2 or the array into the variable myPet
. It does not make a copy of a String. The String object containing the characters "dog" stays in memory where it was previously, but now there are two poiners to it.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
To declare an array, define the variable type with square brackets:
We have now declared a variable that holds an array of strings. To insert values to it, you can place the values in a comma-separated list, inside curly braces:
Array in java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. Following are some important points about Java arrays.
- In Java, all arrays are dynamically allocated. [discussed below]
- Arrays are stored in contiguous memory [consecutive memory locations].
- Since arrays are objects in Java, we can find their length using the object property length. This is different from C/C++, where we find length using sizeof.
- A Java array variable can also be declared like other variables with [] after the data type.
- The variables in the array are ordered, and each has an index beginning from 0.
- Java array can also be used as a static field, a local variable, or a method parameter.
- The size of an array must be specified by int or short value and not long.
- The direct superclass of an array type is Object.
- Every array type implements the interfaces Cloneable and java.io.Serializable.
- This storage of arrays helps us randomly accessing the elements of an array [Support Random Access].
- The size of the array cannot be altered[once initialized]. However, an array reference can be made to point to another array.
An array can contain primitives [int, char, etc.] and object [or non-primitive] references of a class depending on the definition of the array. In the case of primitive data types, the actual values are stored in contiguous memory locations. In the case of class objects, the actual objects are stored in a heap segment.
Creating, initializing, and accessing an Array
One-Dimensional Arrays:
The general form of a one-dimensional array declaration is
type var-name[]; OR type[] var-name;
An array declaration has two components: the type and the name. type declares the element type of the array. The element type determines the data type of each element that comprises the array. Like an array of integers, we can also create an array of other primitive data types like char, float, double, etc., or user-defined data types [objects of a class]. Thus, the element type for the array determines what type of data the array will hold.
Example:
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type
Although the first declaration establishes that int Array is an array variable, no actual array exists. It merely tells the compiler that this variable [int Array] will hold an array of the integer type. To link int Array with an actual, physical array of integers, you must allocate one using new and assign it to int Array.
Instantiating an Array in Java
When an array is declared, only a reference of an array is created. To create or give memory to the array, you create an array like this: The general form of new as it applies to one-dimensional arrays appears as follows:
var-name = new type [size];
Here, type specifies the type of data being allocated, size determines the number of elements in the array, and var-name is the name of the array variable that is linked to the array. To use new to allocate an array, you must specify the type and number of elements to allocate.
Example:
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array
OR
int[] intArray = new int[20]; // combining both statements in one
Note:
The elements in the array allocated by new will automatically be initialized to zero [for numeric types], false [for boolean], or null [for reference types]. Do refer to default array values in Java.
Obtaining an array is a two-step process. First, you must declare a variable of the desired array type. Second, you must allocate the memory to hold the array, using new, and assign it to the array variable. Thus, in Java, all arrays are dynamically allocated.
Array Literal
In a situation where the size of the array and variables of the array are already known, array literals can be used.
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal
- The length of this array determines the length of the created array.
- There is no need to write the new int[] part in the latest versions of Java.
Accessing Java Array Elements using for Loop
Each element in the array is accessed via its index. The index begins with 0 and ends at [total array size]-1. All the elements of array can be accessed using Java for Loop.
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];
Implementation:
Java
var-name = new type [size];1
var-name = new type [size];2
var-name = new type [size];3
var-name = new type [size];4
var-name = new type [size];5
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array0
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array7
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array9
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[20]; // combining both statements in one1
int[] intArray = new int[20]; // combining both statements in one2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int[] intArray = new int[20]; // combining both statements in one4
int[] intArray = new int[20]; // combining both statements in one5
int[] intArray = new int[20]; // combining both statements in one6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[20]; // combining both statements in one8
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];1
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];7
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 505
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 509
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
Student[] arr = new Student[5]; //student is a user-defined class1
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Student[] arr = new Student[5]; //student is a user-defined class4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Student[] arr = new Student[5]; //student is a user-defined class6
Student[] arr = new Student[5]; //student is a user-defined class7
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
Student[] arr = new Student[5]; //student is a user-defined class9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
1
2
3
4
5
6
7
8
9
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Output
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 50
Time Complexity: O[n]
Auxiliary Space: O[1]
You can also access java arrays using for each loops.
Arrays of Objects
An array of objects is created like an array of primitive-type data items in the following way.
Student[] arr = new Student[5]; //student is a user-defined class
Syntax:
1] data type[] arrName;
2] datatype arrName[];
3] datatype [] arrName;
Java
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type03
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type04
var-name = new type [size];4
var-name = new type [size];5
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type11
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type12
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type13
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type15
int[] intArray = new int[20]; // combining both statements in one2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int[] intArray = new int[20]; // combining both statements in one4
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 509
int[] intArray = new int[20]; // combining both statements in one6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type13
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type22
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Output
The student Array contains five memory spaces each of the size of student class in which the address of five Student objects can be stored. The Student objects have to be instantiated using the constructor of the Student class, and their references should be assigned to the array elements in the following way.
Example
Java
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type26
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type27
var-name = new type [size];4
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type29
var-name = new type [size];6
var-name = new type [size];7
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type33
var-name = new type [size];6
var-name = new type [size];7
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type36
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type38
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type40
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type44
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type45
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type44
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type48
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type52
var-name = new type [size];7
var-name = new type [size];4
var-name = new type [size];5
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array0
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type66
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type68
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[20]; // combining both statements in one1
int[] intArray = new int[20]; // combining both statements in one2
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type72
int[] intArray = new int[20]; // combining both statements in one5
int[] intArray = new int[20]; // combining both statements in one6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[20]; // combining both statements in one8
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
int[] intArray = new int[20]; // combining both statements in one2
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type38
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal9
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type85
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type86
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
int[] intArray = new int[20]; // combining both statements in one2
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type38
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];7
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type97
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type86
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];7
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
int[] intArray = new int[20]; // combining both statements in one2
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type38
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
var-name = new type [size];09
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type86
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
int[] intArray = new int[20]; // combining both statements in one2
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type38
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 509
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
var-name = new type [size];19
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type86
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 509
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
int[] intArray = new int[20]; // combining both statements in one2
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type38
int[] intArray = new int[20]; // combining both statements in one5
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
var-name = new type [size];29
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type86
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Student[] arr = new Student[5]; //student is a user-defined class4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Student[] arr = new Student[5]; //student is a user-defined class6
Student[] arr = new Student[5]; //student is a user-defined class7
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
Student[] arr = new Student[5]; //student is a user-defined class9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
1
2
3
var-name = new type [size];42
var-name = new type [size];43
8
6
var-name = new type [size];46
var-name = new type [size];47
6
var-name = new type [size];49
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Output
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type0
Time Complexity: O[n]
Auxiliary Space : O[1]
Example
An array of objects is also created like :
Java
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type26
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type27
var-name = new type [size];55
var-name = new type [size];4
var-name = new type [size];57
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
var-name = new type [size];59
var-name = new type [size];6
var-name = new type [size];7
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type36
var-name = new type [size];6
var-name = new type [size];64
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type44
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type48
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type13
var-name = new type [size];73
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];76
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
var-name = new type [size];78
var-name = new type [size];79
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
var-name = new type [size];55
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type52
var-name = new type [size];7
var-name = new type [size];4
var-name = new type [size];87
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
var-name = new type [size];93
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
var-name = new type [size];96
var-name = new type [size];97
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
var-name = new type [size];99
int[] intArray = new int[20]; // combining both statements in one2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array01
int[] intArray = new int[20]; // combining both statements in one2
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type38
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array04
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array05
int[] intArray = new int[20]; // combining both statements in one2
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type38
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array08
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array05
int[] intArray = new int[20]; // combining both statements in one2
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type38
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array12
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array05
int[] intArray = new int[20]; // combining both statements in one2
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type38
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array16
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array17
var-name = new type [size];55
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Student[] arr = new Student[5]; //student is a user-defined class4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Student[] arr = new Student[5]; //student is a user-defined class6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array23
2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array25
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Output
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type1
What happens if we try to access elements outside the array size?
JVM throws ArrayIndexOutOfBoundsException to indicate that the array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of an array.
Below code shows what happens if we try to access element outside the array size.
Java
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array31
var-name = new type [size];7
var-name = new type [size];4
var-name = new type [size];5
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array0
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array44
int[] intArray = new int[20]; // combining both statements in one2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int[] intArray = new int[20]; // combining both statements in one4
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 509
int[] intArray = new int[20]; // combining both statements in one6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];1
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];7
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 505
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
3
2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array77
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type86
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array80
int[] intArray = new int[20]; // combining both statements in one5
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array82
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Java
var-name = new type [size];7
var-name = new type [size];4
var-name = new type [size];5
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array0
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array44
int[] intArray = new int[20]; // combining both statements in one2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int[] intArray = new int[20]; // combining both statements in one4
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];7
int[] intArray = new int[20]; // combining both statements in one6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal0
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal2
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];1
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Student[] arr = new Student[5]; //student is a user-defined class6
Student[] arr = new Student[5]; //student is a user-defined class7
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
Student[] arr = new Student[5]; //student is a user-defined class9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
1
2
int[] intArray = new int[20]; // combining both statements in one24
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Output
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type2
Multidimensional Arrays:
Multidimensional arrays are arrays of arrays with each element of the array holding the reference of other arrays. These are also known as Jagged Arrays. A multidimensional array is created by appending one set of square brackets [[]] per dimension.
Synatx :
data type [][] arrayrefvaribale;
or
datatype arrayrefvariable[][];
Syntax:
Java
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type03
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type04
var-name = new type [size];4
var-name = new type [size];5
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type11
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type13
int[] intArray = new int[20]; // combining both statements in one38
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type12
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int[] intArray = new int[20]; // combining both statements in one41
int[] intArray = new int[20]; // combining both statements in one2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int[] intArray = new int[20]; // combining both statements in one4
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[20]; // combining both statements in one46
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[20]; // combining both statements in one6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type13
int[] intArray = new int[20]; // combining both statements in one50
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Output
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type4
Example
Java
var-name = new type [size];7
var-name = new type [size];4
int[] intArray = new int[20]; // combining both statements in one56
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
int[] intArray = new int[20]; // combining both statements in one61
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[20]; // combining both statements in one65
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int[] intArray = new int[20]; // combining both statements in one68
2
int[] intArray = new int[20]; // combining both statements in one70
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];7
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
int[] intArray = new int[20]; // combining both statements in one73
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
int[] intArray = new int[20]; // combining both statements in one75
int[] intArray = new int[20]; // combining both statements in one76
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
int[] intArray = new int[20]; // combining both statements in one79
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal9
int[] intArray = new int[20]; // combining both statements in one76
int[] intArray = new int[20]; // combining both statements in one73
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 509
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];7
int[] intArray = new int[20]; // combining both statements in one88
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[20]; // combining both statements in one90
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Student[] arr = new Student[5]; //student is a user-defined class6
Student[] arr = new Student[5]; //student is a user-defined class7
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
Student[] arr = new Student[5]; //student is a user-defined class9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
int[] intArray = new int[20]; // combining both statements in one97
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[20]; // combining both statements in one99
2
Student[] arr = new Student[5]; //student is a user-defined class6
Student[] arr = new Student[5]; //student is a user-defined class7
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal04
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal06
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal08
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal09
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal10
var-name = new type [size];47
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type86
2
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal14
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Output
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type5
Passing Arrays to Methods
Like variables, we can also pass arrays to methods. For example, the below program passes the array to method sum to calculate the sum of the array’s values.
Java
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal20
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal21
var-name = new type [size];7
var-name = new type [size];4
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal24
var-name = new type [size];6
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal26
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
int[] intArray = new int[20]; // combining both statements in one61
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal36
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal9
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];7
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
int[] intArray = new int[20]; // combining both statements in one5
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 509
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal46
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal48
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal50
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal57
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal59
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal63
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal66
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal4
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Student[] arr = new Student[5]; //student is a user-defined class6
Student[] arr = new Student[5]; //student is a user-defined class7
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
Student[] arr = new Student[5]; //student is a user-defined class9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
1
2
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal77
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
3
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal80
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal81
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Output
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type6
Time Complexity: O[n]
Auxiliary Space : O[1]
Returning Arrays from Methods
As usual, a method can also return an array. For example, the below program returns an array from method m1.
Java
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal20
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal86
var-name = new type [size];4
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal24
var-name = new type [size];6
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal26
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
int[] intArray = new int[20]; // combining both statements in one61
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];00
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Student[] arr = new Student[5]; //student is a user-defined class6
Student[] arr = new Student[5]; //student is a user-defined class7
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
Student[] arr = new Student[5]; //student is a user-defined class9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
1
2
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];09
var-name = new type [size];47
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type86
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];18
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];22
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
var-name = new type [size];78
int[] intArray = new int[20]; // combining both statements in one2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];27
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal9
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];7
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal46
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Output
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type7
Time Complexity: O[n]
Auxiliary Space : O[1]
Class Objects for Arrays
Every array has an associated Class object, shared with all other arrays with the same component type.
Java
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal20
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];38
var-name = new type [size];4
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal24
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
int[] intArray = new int[20]; // combining both statements in one61
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];50
int[] intArray = new int[20]; // combining both statements in one2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
int[] intArray = new int[20]; // combining both statements in one4
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[20]; // combining both statements in one6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];57
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];58
int[] intArray = new int[20]; // combining both statements in one2
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];57
int[] intArray = new int[20]; // combining both statements in one4
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[20]; // combining both statements in one6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];65
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];66
int[] intArray = new int[20]; // combining both statements in one2
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];65
int[] intArray = new int[20]; // combining both statements in one4
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[20]; // combining both statements in one6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];73
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];75
int[] intArray = new int[20]; // combining both statements in one2
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];77
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[20]; // combining both statements in one6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];81
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
3
2
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];85
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];87
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];89
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];91
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Output
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type8
Explanation:
- The string “[I” is the run-time type signature for the class object “array with component type int.”
- The only direct super class of an array type is java.lang.Object.
- The string “[B” is the run-time type signature for the class object “array with component type byte.”
- The string “[S” is the run-time type signature for the class object “array with component type short.”
- The string “[L” is the run-time type signature for the class object “array with component type of a Class.” The Class name is then followed.
Array Members
Now, as you know that arrays are objects of a class, and a direct superclass of arrays is a class Object. The members of an array type are all of the following:
- The public final field length, which contains the number of components of the array. Length may be positive or zero.
- All the members inherited from class Object; the only method of Object that is not inherited is its clone method.
- The public method clone[], which overrides the clone method in class Object and throws no checked exceptions.
Arrays Types and Their Allowed Element Types
Array TypesAllowed Element TypesPrimitive Type ArraysAny type which can be implicitly promoted to declared type.Object Type ArraysEither declared type objects or it’s child class objects.Abstract Class Type ArraysIts child-class objects are allowed.Interface Type ArraysIts implementation class objects are allowed.Cloning of arrays
When you clone a single-dimensional array, such as Object[], a “deep copy” is performed with the new array containing copies of the original array’s elements as opposed to references.
Java
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal20
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];96
var-name = new type [size];4
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal24
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
int[] intArray = new int[20]; // combining both statements in one61
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5008
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal9
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];7
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal46
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5017
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5019
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5021
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5023
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Student[] arr = new Student[5]; //student is a user-defined class6
Student[] arr = new Student[5]; //student is a user-defined class7
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
Student[] arr = new Student[5]; //student is a user-defined class9
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5030
2
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5032
var-name = new type [size];47
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type86
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Output
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type9
A clone of a multi-dimensional array [like Object[][]] is a “shallow copy,” however, which is to say that it creates only a single new array with each element array a reference to an original element array, but subarrays are shared.
Java
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal20
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5041
var-name = new type [size];4
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal24
var-name = new type [size];6
var-name = new type [size];7
var-name = new type [size];8
var-name = new type [size];9
int[] intArray = new int[20]; // combining both statements in one61
var-name = new type [size];6
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array2
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5053
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal9
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
// accessing the elements of the specified array for [int i = 0; i < arr.length; i++] System.out.println["Element at index " + i + " : "+ arr[i]];7
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 503
int[] intArray = new int[20]; // combining both statements in one76
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 509
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type84
int[] intArray = new int[20]; // combining both statements in one5
int[] intArray = new int[20]; // combining both statements in one88
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array6
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5066
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5068
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5023
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5072
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5074
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5076
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5078
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal1
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array82
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array3
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5076
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal9
Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 5078
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal9
int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array82
var-name = new type [size];6
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
// both are valid declarations int intArray[]; or int[] intArray; byte byteArray[]; short shortsArray[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[]; // an array of references to objects of // the class MyClass [a class created by // user] MyClass myClassArray[]; Object[] ao, // array of Object Collection[] ca; // array of Collection // of unknown type01
Output
var-name = new type [size];0
Related Articles:
- Jagged Array in Java
- For-each loop in Java
- Arrays class in Java
This article is contributed by Nitsdheerendra and Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.