将两个数组对象一起添加

时间:2017-11-02 14:30:27

标签: java math arraylist binary

我正在尝试在add(DNumber b)方法中添加两个DNumber对象。关键是能够进行二进制算术。元素正常存储。我该如何处理不均匀的ArrayLists?用0s填充少数元素的那个?那么我应该如何检索每个元素?另外,在不使用convert to decimal方法的情况下转换为十进制的好方法是什么:

public class DNumber{
    ArrayList<Digit> binary = new ArrayList<Digit>();
    /**
     * Constructor for objects of class DNumber
     */
    public DNumber()
    {
        Digit num = new Digit(0);
        binary.add(num);
    }

    public DNumber(int val){
        int num = val;
        if(num > 0){
            while (num > 0){
                Digit bin = new Digit(num%2);
                num /= 2;
                binary.add(0, bin);
            }
        }
        else{
            Digit bin = new Digit(0);
            binary.add(0,bin);
        }
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public String toString(){
        String s = "";
        for(Digit d : binary){
            s = s + d.toString();
        }
        return s;
    }

    public void add(DNumber b){
        int ArraySize1 = binary.size() -1;
        int ArraySize2 = b.binary.size() -1;


    }

    public void toDecimal(){
        /**
         *
         *  String s = "";
         int result = 0;
         int power = 0;
         for(Digit d : binary){
         s = s + d.toString();
         result = Integer.parseInt(s);
         }
         */
    }
}

public class Digit {
    int x = 0;

    /**
     * Constructor for objects of class Digit
     */
    public Digit(int val) {
        if (val != 0 && val != 1) {
            System.out.println("Error Must be either 1 or 0");
            x = 0;
        } else {
            x = val;
        }
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param y a sample parameter for a method
     * @return the sum of x and y
     */
    public int getValue() {
        return x;
    }

    public void setValue(int num) {
        if (num != 0 && num != 1) {
            System.out.println("Error Must be either 1 or 0");
            System.out.println("Old Value Retained");
        } else {
            x = num;
        }
    }

    public String toString() {
        return Integer.toString(x);
    }

    public Digit add(Digit b) {
        int returnInt = getValue() + b.getValue();
        Digit carry = new Digit(0);
        if (returnInt == 2) {
            carry = new Digit(1);
            setValue(0);
        } else if (returnInt == 1) {
            carry = new Digit(0);
            setValue(1);
        } else if (returnInt == 0) {
            carry = new Digit(0);
            setValue(0);
        }
        return carry;
    }

    public Digit add(Digit b, Digit c) {
        int returnInt = getValue() + b.getValue() + c.getValue();
        Digit carry = new Digit(0);
        if (returnInt == 2) {
            carry = new Digit(1);
            setValue(0);
        } else if (returnInt == 1) {
            carry = new Digit(0);
            setValue(1);
        } else if (returnInt == 0) {
            carry = new Digit(0);
            setValue(0);
        } else if (returnInt == 3) {
            carry = new Digit(1);
            setValue(1);
        }
        return carry;
    }
}

1 个答案:

答案 0 :(得分:0)

考虑对构造函数的这个小改动。

while (num > 0){
    Digit bin = new Digit(num%2);
    num /= 2;
    binary.add(bin);
}

起初这可能看起来很奇怪,新的DNumber(6)会给你一个列表(0,1,1),这个列表看起来倒退,但更容易使用。

您可以轻松将其转换为小数:

    public int toDecimal() {
        int total = 0;
        int power = 1;
        for (int i = 0; i < binary.size(); i++) {
            total += binary.get(i).getValue() * power;
            power *= 2;
        }
        return total;
    }

当谈到添加你从第0个元素开始,进位为0时,如果一个数组比另一个更长,则更容易处理。

考虑添加6和4

carry = 0
a = (0,1,1)
b = (0,0,1)
answer = ()

开始时,它是0 + 0 + 0 = 0,进位0

carry=0
a = (1,1)
b = (0,1)
answer = (0)

下一次交互,它是0 + 1 + 0 = 1,携带0

carry=0
a = (1)
b = (1)
answer = (0,1)

下一次迭代,它是0 + 1 + 1 = 0,携带1

carry=1
a = ()
b = ()
answer = (0,1,0)

下一次迭代两个输入列表都是空的,所以我们只需添加进位

answer =(0,1,0,1),如果你运行toDecimal是10