• Array is collection of similar data types.
  • Arrays can hold collection of data with indexes
  • We already know that we can hold group of primitive variables
  • Arrays can hold referenced variables also like Strings and objects
  • So we can say it is possible to store or create array of objects in java



Declaring arrays in java:

  •  One dimensional array can be created like
  • int[] singlearray;
  • Two dimensional array can be created like 
  • int[][] intdoubleArray;       
  • double[][] doubleArray;

Instantiation and Initialization of Arrays

Program #1: Java example program to store string object in array
  1. package arraysofobjectsinjava;
  2. public class ArrayOfObjects {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      *  creating and assigning values to arrays in java
  7.      */
  8.    public static void main(String[] args) {
  9.         
  10.         int[] a= new int[2];
  11.         a[0]=1;
  12.         a[1]=2;
  13.         
  14.         System.out.println(a[0]);
  15.         System.out.println(a[1]);
  16.         
  17.         int[] var= {1,2,3,4,5};
  18.         
  19.         
  20.         for (int i = 0; i < var.length; i++) {
  21.             System.out.println(var[i]);
  22.         }      
  23. int[][] array=new int[2][2];       
  24.         
  25.         array[0][0]=1;
  26.         array[0][1]=2;
  27.         array[1][0]=3;
  28.         array[1][1]=4;
  29.         
  30.   for(int i=0; i<array.length; i++) {
  31.  
  32.                for(int j=0; j<array[1].length; j++)
  33.                    System.out.print(array[i][j] + " ");
  34.                System.out.println();
  35. }  
  36.  
  37. }
  38.  
  39. }

 Output:


  1. 1
  2. 2
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. 1 2 
  9. 3 4

Array of objects in java:

Program #2: Java example program to store string object in array


  1. package arraysofobjectsinjava;
  2. public class ArrayofStringObjects {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      * Storing String objects in String array
  7.      */
  8.     public static void main(String[] args) {
  9.         
  10.         
  11.         String a[]= new String[5];
  12.         a[0]="array of objects";
  13.         a[1]="object array in java";
  14.         a[2]="array of objects in java example program";
  15.         a[3]="array of objects in java tutorial";
  16.         a[4]="how to make array of objects in java";
  17.         
  18.         for (int i = 0; i < a.length; i++) {
  19.             System.out.println(a[i]);
  20.         }
  21.  
  22. }
  23.  
  24. }

Output:
  1. array of objects
  2. object array in java
  3. array of objects in java example program
  4. array of objects in java tutorial
  5. how to make array of objects in java

Creating custom array of objects in java
  •  We can also store custom objects in arrays .
  • Create a employee class.
  • Create multiple objects of employee class and assign employee objects to array.
  • Arrays can store objects but we need to instantiate each and every object and array can store it

Program#3: java example program to create custom objects and store in array


Employee.java

  1. package arraysofobjectsinjava;
  2. public class Employee {
  3.  
  4.     String name;
  5.     int id;
  6.     
  7.     Employee(String name, int id){
  8.         this.name=name;
  9.         this.id=id;
  10.         
  11.     }
  12.  
  13. }


array of objects in java

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu