Sergey
Sergey

Reputation: 109

arrays inside of the list

I tried to find what's wrong with this simple code, I tried to search on this website for sample but could't find it. I want to create list of arrays. So when I call myList.get(0) it will output first array1. And if myList.get(0)[0] the very first value. Thank you My code:

List<int[]> myList = new ArrayList<int[]>();
    int[] array1 =  {15, 20, 40};
    int[] array2 =  {30, 7, 18};

    myList.add(array1);
    myList.add(array2);
    System.out.println(myList);

Gives this kind of output. [[I@129f3b5, [I@13f3045]

Upvotes: 0

Views: 6337

Answers (8)

Blehmergh
Blehmergh

Reputation: 11

The code is correct. However, you have to remember that you can't simple print an array, but have to do something like System.out.println(Arrays.toString(myList));

Upvotes: 1

Plux
Plux

Reputation: 412

This will first print out each value on a new line, now you should probably edit the output by yourself. And the second loop will print out each table using Arrays.toString()

public static void main(String[] args) {
    List<int[]> myList = new ArrayList<int[]>();
    int[] array1 = { 15, 20, 40 };
    int[] array2 = { 30, 7, 18 };

    myList.add(array1);
    myList.add(array2);
    for (int[] tables : myList)
        for (int i : tables)
            System.out.println(i);
    //This is also possible
    for (int[] tables : myList)
        System.out.println(Arrays.toString(tables));

}

Upvotes: 2

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

You can use Arrays.toString() to print the array. ;

System.out.printlin(myList.get(0)) // first Array from the list
System.out.printlin(myList.get(1)) // second Array from the list

Use below code to print entire list.

   for(int[] array : myList) {
          System.out.println(Arrays.toString(array));
   }

Upvotes: 1

Maroun
Maroun

Reputation: 95948

You should print it like that:

for(int i = 0; i < myList.size(); i++) {
    int[] numbers = myList.get(i);
    System.out.println(Arrays.toString(numbers));
}

Array is Object, and as you know, each object has toString method. The default toString displays the class name representation, then adds @ and then the hashcode.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691665

Unfortunately, Java arrays don't have a good toString() method that would return a well-formatted view of the elements it contains. And System.out.println(Object) actually calls this bad toString() method, which simply returns the type of the array ([[I) followed by its hash code.

To get a meaningful representation of an array, use java.util.Arrays.toString():

for (int[] array : myList) {
    System.out.println(Arrays.toString(array);
}

Upvotes: 1

nachokk
nachokk

Reputation: 14413

You have to do something like this, import java.util.Arrays and use this static method Arrays#toString(int[])

for(int[] a : myList) {
   System.out.println(Arrays.toString(a));
}

Upvotes: 5

Eel Lee
Eel Lee

Reputation: 3543

In your example myList.get(0) will return an array of ints, because that's what your list contains.

So if you want to print all the elements of the array you should do something like

for (int[] array : myList) {
    for (Integer i : array) {
        System.out.println(i);
    }
}

Upvotes: 2

Jan Zyka
Jan Zyka

Reputation: 17898

The

I@129f3b5

I@13f3045

are the default toString() implementations for arrays.

So the code is completely correct, the arrays are stored in the list. Just the toString() is not of much use for arrays.

Simply - you should not rely on array toString().

Upvotes: 0

Related Questions