
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
Java Program to Access all Data as Object Array
Array is a linear data structure that is used to store a group of elements with the same data type.
We can create an array with primitive datatypes, and since a class is considered a user-defined datatype in Java, it is also possible to create an array of objects.
In this article, we are going to discuss object arrays. First of all, let's discuss what is an object array, in Java.
What is Object array or Array of Objects
An array of objects contains reference variables of objects.
We follow the same syntax to create an array of primitives and an array of objects. However, instead of a primitive datatype, we use a class name in the case of an object array.
Syntax for Array of Objects
The code below shows the syntax of the declaration and initialization of an object array:
// declaration Class_name objectArray[]; Or, // declaration and instantiation Class_name objectArray[] = new Class_name[sizeofarray];
To create an array of objects that can store 5 objects of class Cart, use the class name instead of a primitive datatype as shown below:
Cart[ ] obj = new Cart[5];
Adding values to Object Array
Remember, when we declare and initialize an 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 the object array, we need to initialize the elements of the 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.
Program to access an Object Array
In Java, we can use either a for loop or a while loop to access elements of an.
Example 1: Using while Loop
In the following example, we will create an array of objects and initialize it with values by using the constructor. Then, we use the while loop to access the values.
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 } } }
On running, the above code will display the data stored in the object array.
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
Example 2: Using a for Loop
The following example illustrates how to use a for loop to access data as an 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); } } }
Following is the output of the above program -
Item: Rice, Price: 59.0 Item: Milk, Price: 60.0 Item: Bread, Price: 45.0