2

I'm trying to understand some basics about the ArrayList, and in general, about Collection Frameworks.

In the code to follow, what I am trying to achieve is, to store a bunch of ArrayLists of different DataTypes inside another ArrayList (similar to the concept of a multidimensional array), and then trying to retrieve the data.

ArrayLists inside an ArrayList

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Testing {

    private static Scanner sc;
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) {

        ////////////////////////////////////Variables/////////////////////////////////////

        String[] string;
        Integer[] integer = new Integer[5];
        Boolean[] bool = new Boolean[5];
        Byte[] by = new Byte[5];
        Double[] doub = new Double[5];

        sc = new Scanner(System.in);


        /////////////////////////////////////Inputs///////////////////////////////////////

        System.out.println("Enter an Array of String : ");
        string = sc.nextLine().split("[ ]");                //Entering dynamic String inputs for the String Types

        System.out.println("Enter an Array of Integer : ");
        for (int i = 0; i < integer.length; i++) {
            integer[i] = sc.nextInt();                      //Entering dynamic Integer inputs for the Integer Types
        }

        System.out.println("Enter an Array of Boolean : ");
        for (int i = 0; i < bool.length; i++) {
            bool[i] = sc.nextBoolean();                     //Entering dynamic Boolean inputs for the Boolean Types
        }

        System.out.println("Enter an Array of Byte : ");
        for (int i = 0; i < by.length; i++) {
            by[i] = sc.nextByte();                          //Entering dynamic Byte inputs for the Byte Types
        }

        System.out.println("Enter an Array of Double : ");
        for (int i = 0; i < doub.length; i++) {
            doub[i] = sc.nextDouble();                      //Entering dynamic Double inputs for the Double Types
        }


        ////////////////////////////////////Operations///////////////////////////////////////

        ArrayList a1 = new ArrayList<String>();
        a1.add(string);                                 //Inserting string array into ArrayList a1

        ArrayList a2 = new ArrayList<Integer>();
        a2.add(integer);                                //Inserting integer array into ArrayList a2

        ArrayList a3 = new ArrayList<Boolean>();
        a3.add(bool);                                   //Inserting bool array into ArrayList a3

        ArrayList a4 = new ArrayList<Byte>();
        a4.add(by);                                     //Inserting by array into ArrayList a4

        ArrayList a5 = new ArrayList<Double>();
        a5.add(doub);                                   //Inserting doub array into ArrayList a5

        ArrayList AL = new ArrayList<>();
        AL.add(a1);                                     //Adding ArrayList a1 to the ArrayList AL
        AL.add(a2);                                     //Adding ArrayList a2 to the ArrayList AL
        AL.add(a3);                                     //Adding ArrayList a3 to the ArrayList AL
        AL.add(a4);                                     //Adding ArrayList a4 to the ArrayList AL
        AL.add(a5);                                     //Adding ArrayList a5 to the ArrayList AL

        System.out.println("Done");

        /////////////////////////////////////Displaying Output/////////////////////////////////////

        for (Object object : AL) {
            Iterator<ArrayList> i = ((ArrayList)object).iterator();
            while(i.hasNext())                          //Run while the individual ArrayList has more elements left
                System.out.println(i.next());
        }
    }
}

Following are the inputs I have provided

Enter an Array of String : 
1 2 3 4 5
Enter an Array of Integer : 
1 2 3 4 5
Enter an Array of Boolean : 
true true true true true
Enter an Array of Byte : 
1 2 3 4 5
Enter an Array of Double : 
1 2 3 4 5
Done

Output

[Ljava.lang.String;@1e643faf
[Ljava.lang.Integer;@6e8dacdf
[Ljava.lang.Boolean;@7a79be86
[Ljava.lang.Byte;@34ce8af7
[Ljava.lang.Double;@b684286

My question is, why am I getting the hashcode values for the individual ArrayList where instead I was expecting the data inside the individual array lists?

I have tried using Arrays.deepToString() method, but it doesn't work.

I know the code isn't perfect and that there is probably a better way to do what I'm trying to do here, but I would deeply appreciate any hints/guidance on this.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

2

Update Live working example: https://ideone.com/uNgGQ0


The problem here is that Since you are storing different types of ArrayList<DifferentDataType> into the ArrayList<Object> and the case with ArrayList is that they store the data ultimately inside the Array.

So it's not straightforward to traverse them like how we used to traverse the List of the single data type. I have tweaked your code a little and it's working fine now.

        for (Object object : AL) {
            ArrayList temp = (ArrayList) object;
            for (Object innerObject : temp) {
                if (innerObject instanceof String[]) {
                    printArray((String[]) innerObject);
                } else if (innerObject instanceof Integer[]) {
                    printArray((Integer[]) innerObject);
                } else if (innerObject instanceof Byte[]) {
                    printArray((Byte[]) innerObject);
                } else if (innerObject instanceof Boolean[]) {
                    printArray((Boolean[]) innerObject);
                } else if (innerObject instanceof Double[]) {
                    printArray((Double[]) innerObject);
                }
            }
        }

 private static void printArray(Object[] obj) {
        for (Object ob : obj) {
            System.out.print(ob + ",");
        }
        System.out.println();
    }

Output

Enter an Array of String : 
1 2 3 4 5
Enter an Array of Integer : 
1 2 3 4 5
Enter an Array of Boolean : 
true true true true true
Enter an Array of Byte : 
1 2 3 4 5
Enter an Array of Double : 
1 2 3 4 5
Done
1,2,3,4,5,
1,2,3,4,5,
true,true,true,true,true,
1,2,3,4,5,
1.0,2.0,3.0,4.0,5.0,
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
  • `Enter an Array of String : 1 2 3 4 5 Enter an Array of Integer : 1 2 3 4 5 Enter an Array of Boolean : true true true true true Enter an Array of Byte : 1 2 3 4 5 Enter an Array of Double : 1 2 3 4 5 Done **Exception in thread "main" java.lang.ClassCastException: java.base/[Ljava.lang.String; cannot be cast to java.base/java.util.List at com.jlc.etc2.Testing.main(Testing.java:80)**` – 404 Brain Not Found Jul 31 '18 at 03:18
  • Got it. Thank You. Also, one quick question. Why do I need to check the `instanceOf` the `innerObject` ? I mean, so far the only reason I can see is to typeCast it into a `dataType[]`, but even that seems kinda unnecessary since all we are doing inside `printArray()` is taking the parameter as `Object` and then printing it. – 404 Brain Not Found Jul 31 '18 at 03:47
  • @404BrainNotFound No need to downcast, I just did if you want to perform some specific operation you can leave it if not needed. – Neeraj Jain Jul 31 '18 at 05:21