
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Access All Data as Object Array in Java
Array is a linear data structure that is used to store group of elements with similar datatypes. We can create an array with primitive datatypes and since a class is considered as user-defined datatype so it is also possible to create array of objects.
In this article, we are going to discuss object array and we will create java program to access all data as object array.
Object array or Array of Objects
Array of objects actually contains reference variables of objects i.e. the elements stored in object array are of reference type. We follow the same syntax to create array of primitives and array of objects. However, instead of primitive datatype we use class name in case of object array.
Syntax for Array of Primitives
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};
Instance
String[] item = new String[5];
In the above example, we have created a array of string that can store 5 string elements.
Syntax for array of object
Class_name objectArray[]; // declaration Or, // declaration and instantiation Class_name objectArray[] = new Class_name[sizeofarray];
Instance
Cart[ ] obj = new Cart[5];
In the above example, we have created an array of object that can store 5 objects of class Cart. Instead of primitive datatype we have used the class name.
Remember, when we declare and initialize object array, it does not create objects for the elements automatically rather we need to create objects separately for every element.
After the instantiation of object array we need to initialize the elements of array with the values. In this case, objects are the elements. One way to pass the values is by using the constructor of the class or we can we can create multiple objects and then pass them to another object array.
Syntax
arrayObject_name[index] = new constructor_name( values ); Or, arrayObject_name[index] = object_name;
We will see the examples in the next section.
Java program to access all data as Object Array
Example 1
In the following example, we will create an array of objects and initialize it with values by 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 } } }
Output
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 have created class ?Cart' and its constructor ?Cart' which is accepting two parameters ?item' and ?price'. In the main method, we have created array of object ?obj' of size 5 of class ?Cart'. By using the constructor ?Cart', the elements of array are initialized. We have used the while loop to print the values.
Example 2
The following example illustrates another way of accessing data as object array.
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); } } }
Output
Item: Rice, Price: 59.0 Item: Milk, Price: 60.0 Item: Bread, Price: 45.0
In the above example, we have created class ?Cart' and its three objects ?c1', ?c2', ?c3'. Also we have created an array of object ?obj' of size 3 of class ?Cart'. By using the objects the elements of array are initialized. We have used the for loop to print the values.
Conclusion
In this article, we have understood the similarities and differences between array of primitives and array of objects. With the help of two java programs, we have discussed how we can access data as object array.