public class Square {
private final TreeMap<Integer,TreeMap<Integer,Double>> square;
private final int height;
private final int width;
public Square(int h, int w) {
this.square = new TreeMap<>();
this.height = h;
this.width = w;
}
public boolean isIdentity() {
boolean isIdentity = false;
for (Integer key1 : square.keySet()) { // First integer key of treemap (row)
for (Integer key2 : square.keySet()) { // Second integer key (column)
for(TreeMap<Integer, Double> value : matrix.values()) {
if ((key1.intValue() == key2.intValue()) && (key2.intValue() == 1.0)) {
isIdentity = true;
}
else {
isIdentity = false;
}
}
}
}
return isIdentity;
}
私が見ようとしているのは、正方形が恒等正方形になることです(下)。私が抱えている問題は、(私が思うに)「キー」を正しく並べることです。私の考えでは、(key1 / key2) の double 値は 1.0 である必要があります
身元:
1000000
0100000
0010000
0001000
0000100
0000010
0000001
(keySet() は null ですか?)
テスト:
public static void main(String [] args) {
HashMap<String,Square> square =
new HashMap<String,Square>();
Scanner input = new Scanner(System.in);
System.out.print("Enter a command: ");
String cmd = input.next();
while (!cmd.equals("end")) {
if (cmd.equals("new")) {
String name = input.next();
int rows = input.nextInt();
int cols = input.nextInt();
if (rows < 1 || cols < 1) {
System.out.println("new: rows and/or cols less than 1: ");
System.exit(1);
}
Square m = new Square(rows,cols);
int i = input.nextInt();
while (i >= 0) {
int j = input.nextInt();
double v = input.nextDouble();
m.set(i,j,v);
i = input.nextInt();
}
square.put(name,m);
System.out.printf("new %s = %s\n", name, m);
}
if (cmd.equals("isIdentity")) {
String which = input.next();
if (!square.containsKey(which)) {
System.out.println("isIdentity: no such matrix: " + which);
System.exit(1);
}
System.out.printf("%s.isIdentity() = %b\n",
which, square.get(which).isIdentity());
}
}
} // end of main method
入力を次のようにテストします。
Enter a command:
new one 1000 2000
0 0 1.0
50 834 5.0
-1
Enter a command:
isIdentity one