I have an ArrayList that I want to print in decimal and binary format.
Current output: decimal: S:2 S:3 S:3 S:3 S:1 S:2
Expected:
decimal: 2 3 3 3 1 2 binary : 10 11 11 11 1 10
Any help is appreciated on how I can accomplish this.
I get this error "Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method toBinaryString(int) in the type Integer is not applicable for the arguments (List)at generics.ExampleGenerics.main(ExampleGenerics.java:50)"
I am learning generics, do I need to cast an int to the string array to get this to work? or am I way off?
public class ExampleGenerics {
public static void main(String[] args) {
List < Square > squareList = new ArrayList < > (Arrays.asList(new Square(1),
new Square(2), new Square(2), new Square(3), new Square(3), new Square(3)));
System.out.println("original squareList: " + squareList);
Collections.rotate(squareList, -2);
System.out.println("rotated list: " + squareList);
System.out.println(Integer.toBinaryString(squareList)); //error
}
}
public class Square {
private int side;
public Square(int side) {
this.side = side;
}
public int getSide() {
return side;
}
public void setSide(int side) {
this.side = side;
}
@Override
public String toString() {
return "S:" + side;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + side;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Square))
return false;
Square other = (Square) obj;
if (side != other.side)
return false;
return true;
}
}