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]]
}
}