Note that there are some explanatory texts on larger screens.

plurals
  1. POIterate to find out whether a square is the "identity" - java treemaps
    primarykey
    data
    text
    <pre><code>public class Square { private final TreeMap&lt;Integer,TreeMap&lt;Integer,Double&gt;&gt; square; private final int height; private final int width; public Square(int h, int w) { this.square = new TreeMap&lt;&gt;(); 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&lt;Integer, Double&gt; value : matrix.values()) { if ((key1.intValue() == key2.intValue()) &amp;&amp; (key2.intValue() == 1.0)) { isIdentity = true; } else { isIdentity = false; } } } } return isIdentity; } </code></pre> <p>I'm trying to see is the square is going to be an identity square (below). The problem I'm having is (I think) lining up the "keys" correctly. In my mind, the double value at (key1 / key2 ) should be 1.0</p> <p>Identity:</p> <pre><code>1000000 0100000 0010000 0001000 0000100 0000010 0000001 </code></pre> <p>(keySet() is null? )</p> <p>Testing: </p> <pre><code>public static void main(String [] args) { HashMap&lt;String,Square&gt; square = new HashMap&lt;String,Square&gt;(); 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 &lt; 1 || cols &lt; 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 &gt;= 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 </code></pre> <p>Test input as such: </p> <pre><code>Enter a command: new one 1000 2000 0 0 1.0 50 834 5.0 -1 Enter a command: isIdentity one </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload