An array is a linear data structure used to store groups of elements with similar data types. We can create an array using primitive data types and since classes are considered user-defined data types, we can create arrays of objects as well.
In this article, we will discuss object arrays and create java programs to access all data in the form of object arrays.
The object array actually contains the reference variables of the object, that is, the elements stored in the object array are reference types. We follow the same syntax to create arrays of primitives and arrays of objects. However, in the case of array of objects, we use the class name instead of the primitive data type.
Data_Type[] nameOfarray; // declaration Or, Data_Type nameOfarray[]; // declaration Or, // declaration with size Data_Type nameOfarray[] = new Data_Type[sizeofarray]; // declaration and initialization Data_Type nameOfarray[] = {values separated with comma};
String[] item = new String[5];
In the above example, we created a string array that can store 5 string elements.
Class_name objectArray[]; // declaration Or, // declaration and instantiation Class_name objectArray[] = new Class_name[sizeofarray];
Cart[ ] obj = new Cart[5];
In the above example, we created an object array that can store 5 objects of the Cart class. We use class names instead of primitive data types.
Remember that when we declare and initialize the object array, it does not automatically create objects for the elements, instead we need to create objects for each element individually.
After instantiating the object array, we need to initialize the elements of the array with values. In this case, the objects are elements. One way to pass a value is to use the constructor of a class, or we can create multiple objects and then pass them to another array of objects.
arrayObject_name[index] = new constructor_name( values ); Or, arrayObject_name[index] = object_name;
We will see examples in the next section.
In the following example, we will create an array of objects and initialize it with values using the constructor.
class Cart { String item; double price; Cart(String item, int price) { // Constructor this.item = item; this.price = price; } } public class Main { public static void main(String[] args) { Cart[ ] obj = new Cart[5]; // creation of object array // Passing values to the array object obj[0] = new Cart("Rice", 59); obj[1] = new Cart("Milk", 60); obj[2] = new Cart("Bread", 45); obj[3] = new Cart("Peanut", 230); obj[4] = new Cart("Butter", 55); System.out.println("Accessing data as Object Array: "); int i = 0; // initialization of loop variable while(i < obj.length) { // to iterate through array obejct System.out.println("Item: " +obj[i].item + ", " + "Price: " +obj[i].price); // to print the values i++; // incrementing loop variable } } }
Accessing data as Object Array: Item: Rice, Price: 59.0 Item: Milk, Price: 60.0 Item: Bread, Price: 45.0 Item: Peanut, Price: 230.0 Item: Butter, Price: 55.0
In the above example, we created the class "Cart" and its constructor "Cart", which accepts two parameters "item" and "price". In the main method we create an array of objects "obj" of class "Cart" of size 5. By using the constructor "Cart", the elements of the array are initialized. We use while loop to print the value.
The following example illustrates another way to access data as an array of objects.
class Cart { String item; double price; } public class Arrayobj { public static void main(String []args) { // Initializing the values to the variables Cart c1 = new Cart(); // object 1 c1.item = "Rice"; c1.price = 59; Cart c2 = new Cart(); // object 2 c2.item = "Milk"; c2.price = 60; Cart c3 = new Cart(); // object 3 c3.item = "Bread"; c3.price = 45; Cart obj[] = new Cart[3]; // array of object // Passing objects to object array obj[0] = c1; obj[1] = c2; obj[2] = c3; for(int i = 0; i < obj.length ; i++ ) { System.out.println("Item: " +obj[i].item + ", " + "Price: " +obj[i].price); } } }
Item: Rice, Price: 59.0 Item: Milk, Price: 60.0 Item: Bread, Price: 45.0
In the above example, we created the class "Cart" and its three objects "c1", "c2", "c3". Additionally, we have created an array of objects "obj" of size 3 of class "Cart". By using objects, the elements of the array are initialized. We use for loop to print the values.
In this article, we learned about the similarities and differences between arrays of primitives and arrays of objects. With the help of two java programs, we discussed how to access data in the form of array of objects.
The above is the detailed content of Java program access all data as array of objects. For more information, please follow other related articles on the PHP Chinese website!