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.