Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a more idiomatic way of doing it (using the Cloneable interface):</p> <pre><code>public class Register implements Cloneable { private boolean[] register; public Register(boolean[] register) { int n = register.length; if (n == 8 || n == 16 || n == 32 || n == 64) { this.register = register; } else { throw new IllegalArgumentException( "A register can only contain 8, 16, 32, or 64 bits"); } } @Override public String toString() { StringBuilder builder = new StringBuilder(); for ( boolean b : this.register ) { builder.append( b ? "1" : "0" ); } return builder.toString(); } public Register( int n ) { this( new boolean[n] ); } public int getLength() { return this.register.length; } @Override public Register clone() { boolean[] clonedRegister = new boolean[this.register.length]; System.arraycopy(this.register, 0, clonedRegister,0, this.register.length); return new Register( clonedRegister ); } } </code></pre> <p>And a JUnit test showing it in action:</p> <pre><code>import org.junit.Assert; import org.junit.Test; public class RegisterTest { @Test public void testRegisterToString() { Register source = new Register( new boolean[] {true, true, false, false, true, false, true, false } ); String result = "11001010"; Assert.assertEquals( result, source.toString() ); } @Test public void testRegisterCloning() { Register source = new Register( new boolean[] {true, true, false, false, true, false, false, false } ); Register clone = source.clone(); Assert.assertEquals( source.toString(), clone.toString() ); } } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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