1

So I have this List<> which contains boolean arrays:

static List<boolean[]> myList = new ArrayList<>();

And these are the arrays I add into the list:

boolean[] myBooleanArray = new boolean[8];

So now when I go trough the myList with a for() loop, using this:

myList.get(i);

My output is something like this in console:

[Z@65e7c41f

But I want to into thoes objects too. How do I do this?

Veske
  • 547
  • 1
  • 4
  • 15
  • 1
    This question has been asked tons of times here on SO. You have to use `Arrays.toString(myList.get(i))` or use a `for` loop to print each element of the `boolean` array – BackSlash Apr 21 '14 at 12:10
  • possible duplicate of [Simplest way to print an array in Java](http://stackoverflow.com/questions/409784/simplest-way-to-print-an-array-in-java) – BackSlash Apr 21 '14 at 12:11

2 Answers2

2

As arrays don't override the methods from the Object class, you get the default String representation which is:

getClass().getName() + '@' + Integer.toHexString(hashCode())

If you want to print the content of an array, you can use Arrays.toString().

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


I don't know what you're doing with your List of boolean arrays, but be aware that you can have small surprises:
public class Test {   
    static List<boolean[]> myList = new ArrayList<>();

    public static void main(String[] args){
        boolean[] arr = {true, false};
        boolean[] arr2 = {true, false};

        myList.add(arr);
        System.out.println(myList.contains(arr2)); //false
    }   
}

An alternative is to create a wrapper class that wraps your boolean array and you can use the methods Arrays.equals, Arrays.toString and Arrays.hashcode to implement properly the equals, toString and hashCode methods.

class MyBooleanArray {
    private boolean[] arr;

    public MyBooleanArray(boolean[] arr) {
        this.arr = arr;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + Arrays.hashCode(arr);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MyBooleanArray other = (MyBooleanArray) obj;
        if (!Arrays.equals(arr, other.arr))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "MyBooleanArray [arr=" + Arrays.toString(arr) + "]";
    }   
}

And the main:

public class Test {   
    static List<MyBooleanArray> myList = new ArrayList<>();

    public static void main(String[] args){
        boolean[] arr = {true, false};
        boolean[] arr2 = {true, false};
        myList.add(new MyBooleanArray(arr));
        System.out.println(myList.contains(new MyBooleanArray(arr2))); //true
        System.out.println(myList.get(0)); //MyBooleanArray [arr=[true, false]]
    }   
}
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
1

You are getting the references by myList.get(i).

To see the difference try this:

for(int i=0; i < myList.size(); i++){
    System.out.println(myList.get(i));
    for (int j = 0; j < myList.get(i).length; j++)//+
        System.out.print(myList.get(i)[j]);
}

+: I've shown the elements of the array this way to make it more clear for you how you should access the elements of the array.

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66