java - 将两个数组对象相加

标签 java math arraylist binary

我正在尝试在 add(DNumber b) 方法中添加两个 DNumber 对象。重点是能够进行二进制算术。元素存储正常。我应该如何处理不均匀的ArrayList?元素较少的用0补齐?那么我应该如何检索每个元素呢?另外,在不使用转换为十进制方法的情况下转换为十进制的好方法是什么:

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

最佳答案

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

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

一开始这可能看起来很奇怪,new 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

关于java - 将两个数组对象相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47077778/

相关文章:

c# - 从矩阵变换计算角度

java - 从 ArrayList 中检索随机对象

Java 和数据结构(Map、Hashmap、List、Arraylist...)

Java ArrayList<GenericObject>.remove(GenericObject) 返回 false,但仍减少 ArrayList 的大小

java - 用 java 确定 microsoft office 的版本

Java 8 os.version 不包含整个 Windows 版本

java - 为什么 FileWriter 不写入 "output.csv"但写入确切路径 "C:\\path\\output.csv"?

java - 无法通过蓝牙进行调试

math - 从带有 x 轴的向量计算角度(梯度)

c++ - 在 C++ 中查找 2 个数字的比率