Note that there are some explanatory texts on larger screens.

plurals
  1. POCopy constructor help, trying to copy a boolean array. Java
    primarykey
    data
    text
    <p>I have looked through as many previous questions as possible but never saw a question that had a boolean array as a variable.</p> <p>Here is my class:</p> <pre><code>public class Register { private boolean[] register; private int length; //Normal constructor public Register(int n) { if (n == 8 || n == 16 || n == 32 || n == 64) { length = n; register = new boolean[length]; for (int i = 0; i &lt; length; i++) { register[i] = false; } } else { throw new RegisterException( "A register can only contain 8, 16, 32, or 64 bits"); } } // Creates a copy of reg (an existing Register) public Register(Register reg) { length = reg.length; register = new boolean[reg.register.length]; System.arraycopy(reg.register, 0, this.register, 0, reg.register.length); } </code></pre> <p>In my driver program i am loading "1101101" into register1, but when i do: Register register2 = new Register(register1);</p> <p>and print out both results i get:</p> <p>0000000001101101</p> <p>0000000000010110</p> <p>Not really sure what is going on O.o any help would be appreciated, thanks!</p> <p>This is my load method. i held off on putting it in here because it might be hard to read:</p> <pre><code>public void load(String binaryRep) { String allTheBits = binaryRep; int charPosition = 0; int loadLength; int binaryNum = 0; String index = ""; String trimmedIndex = ""; if (allTheBits.length() &gt; 0 &amp;&amp; allTheBits.length() &lt;= length) { loadLength = allTheBits.length(); for (int i = length - (loadLength); i &lt; length; i++) { index = allTheBits.charAt(charPosition) + ""; trimmedIndex = index.trim(); binaryNum = Integer.parseInt(trimmedIndex); if (binaryNum == 1) { register[i] = true; } else if (binaryNum == 0) { register[i] = false; } charPosition++; } } else { throw new RegisterException("You can only load 0 - " + length + "bits."); } } </code></pre>
    singulars
    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.
 

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