It is possible to create arrays of objects. These may have one or multiple dimensions. To create follow below steps.
1) Declare an array
2) Allocate memory for the array elements
Syntax to create Arrays of Objects
clsName varName[ ] = new clsName[size];
clsName is a name of wrapper class for which you want to create array.
varName is a variable name given to an array.
Provide integer value in place of size to define size of number of elements in an array.
Example of Arrays of Objects
Example 1 : Program to create and display arrays of an object value
class StringArray
{
public static void main(String args[])
{
String strArr[] = new String[2];
strArr[0] = "This is ";
strArr[1] = "an example to ";
strArr[2] = "create an array of objects.";
System.out.print(strArr[0]);
System.out.print(strArr[1]);
System.out.print(strArr[2]);
System.out.println("");
// Or you can use for loop to print array values
for (int i = 0; i < strArr.length; i = i + 1)
{
System.out.print(strArr[i]);
}
}
}
Output
This is an example to create an array of objects.
This is an example to create an array of objects.