Copy the design help while trying to copy a boolean array. Java

I looked through as many previous questions as possible, but never saw a question with a boolean array as a variable.

Here is my class:

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 < 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);
}

In my driver program, I load "1101101" into register1, but when I do: Register register2 = new Register (register1);

and print both results:

0000000001101101

0000000000010110

Not sure if Oo is going on any help would be appreciated, thanks!

This is my download method. I stopped by putting it here because it would be hard to read:

public void load(String binaryRep) {

    String allTheBits = binaryRep;
    int charPosition = 0;
    int loadLength;
    int binaryNum = 0;
    String index = "";
    String trimmedIndex = "";

    if (allTheBits.length() > 0 && allTheBits.length() <= length) {

        loadLength = allTheBits.length();

        for (int i = length - (loadLength); i < 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.");
    }
}
+5
source share
3 answers

Here's a more idiomatic way to do this (using the Cloneable interface):

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 );
}

}

JUnit, :

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() );

    }

}
+4

, .

  • @Ted, .
  • , , . "register", false
  • , , 32 . 64- 32 * 64 + 32 = 2080 ... , , :)

, (BTW, Arrays.copyOf, ), .

+3

I just checked your boot method as follows:

public static void main(String [] args)
{
    Register r1 = new Register(8);
    r1.load("1101101");
    Register r2 = new Register(r1);
    for (int i=0; i<8; i++) System.out.println(r2.register[i]);
}

Conclusion:

> run Register
false
true
true
false
true
true
false
true
> 

It seems to me that this concerns the contents of Register objects, so the problem is probably related to access.

+2
source

All Articles