Java 将两个数组对象添加到一起

Java 将两个数组对象添加到一起,java,math,arraylist,binary,Java,Math,Arraylist,Binary,我试图在add(DNumber b)方法中添加两个DNumber对象。关键是要能做二进制算术。元素存储正常。我应该如何处理不均匀的数组列表?用0s?填充元素较少的元素?。那么我应该如何检索每个元素呢?另外,在不使用convert to decimal方法的情况下,什么是转换为decimal的好方法: public class DNumber{ ArrayList<Digit> binary = new ArrayList<Digit>(); /**

我试图在add(DNumber b)方法中添加两个DNumber对象。关键是要能做二进制算术。元素存储正常。我应该如何处理不均匀的数组列表?用0s?填充元素较少的元素?。那么我应该如何检索每个元素呢?另外,在不使用convert to decimal方法的情况下,什么是转换为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;
    }
}
公共类数据编号{
ArrayList binary=新的ArrayList();
/**
*DNumber类对象的构造函数
*/
公共数据编号()
{
数字num=新数字(0);
添加(num);
}
公共数据编号(int val){
int num=val;
如果(数值>0){
while(num>0){
数字箱=新数字(数字%2);
num/=2;
二进制。添加(0,bin);
}
}
否则{
数字仓=新数字(0);
二进制。添加(0,bin);
}
}
/**
*方法示例-将此注释替换为您自己的注释
*
*@param y方法的示例参数
*@返回x和y的总和
*/
公共字符串toString(){
字符串s=“”;
用于(数字d:二进制){
s=s+d.toString();
}
返回s;
}
公共无效添加(数据编号b){
int ArraySize1=binary.size()-1;
int ArraySize2=b.binary.size()-1;
}
公共无效到指定日期(){
/**
*
*字符串s=“”;
int结果=0;
整数幂=0;
用于(数字d:二进制){
s=s+d.toString();
结果=整数.parseInt(s);
}
*/
}
}
公共类数字{
int x=0;
/**
*类Digit对象的构造函数
*/
公共数字(int-val){
如果(val!=0&&val!=1){
System.out.println(“错误必须为1或0”);
x=0;
}否则{
x=val;
}
}
/**
*方法示例-将此注释替换为您自己的注释
*
*@param y方法的示例参数
*@返回x和y的总和
*/
public int getValue(){
返回x;
}
公共无效设置值(int num){
如果(num!=0&&num!=1){
System.out.println(“错误必须为1或0”);
系统输出打印项次(“保留旧值”);
}否则{
x=num;
}
}
公共字符串toString(){
返回整数.toString(x);
}
公共数字添加(数字b){
int returnInt=getValue()+b.getValue();
数字进位=新数字(0);
if(returnInt==2){
进位=新数字(1);
设定值(0);
}else if(returnInt==1){
进位=新数字(0);
设定值(1);
}else if(returnInt==0){
进位=新数字(0);
设定值(0);
}
返运;
}
公共数字添加(数字b、数字c){
int returnInt=getValue()+b.getValue()+c.getValue();
数字进位=新数字(0);
if(returnInt==2){
进位=新数字(1);
设定值(0);
}else if(returnInt==1){
进位=新数字(0);
设定值(1);
}else if(returnInt==0){
进位=新数字(0);
设定值(0);
}else if(returnInt==3){
进位=新数字(1);
设定值(1);
}
返运;
}
}

考虑一下对构造函数的这一小改动

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+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)
下一次迭代中,两个输入列表都是空的,所以我们只需添加进位


答案=(0,1,0,1),如果您运行toDecimal is 10,请考虑对构造函数的这个小更改

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+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)
下一次迭代中,两个输入列表都是空的,所以我们只需添加进位


答案=(0,1,0,1),如果您运行toDecimal is 10,那么使用Digital类强制1和0并不一定是件坏事,但是在Digital类上使用add方法没有多大意义,因为您实际上无法处理进位。我将重点关注在DNumber级别处理add,并将第0个元素设为0位可能会使事情变得更简单。使用Digit类强制1和0并不一定是坏事,但是在Digit类上使用add方法没有多大意义,因为您实际上无法处理进位。我将着重于在DNumber级别处理add,并将第0个元素设为0位可能会使事情变得更简单