简体   繁体   中英

Not able to return objects in an ArrayList Java

I believe it has to do with my constructor but I am not entirely sure. public String toString() is supposed to iterate through the array and return the result. This converts decimal to binary.

public class DNumber {

    ArrayList<Digit> binary = new ArrayList<Digit>();

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

    public DNumber(int val) {
        ArrayList<Digit> binary = new ArrayList<>();
        int num = val;
        while (num > 0) {
            Digit bin = new Digit(num % 2);
            num /= 2;
            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() {
        ArrayList<Digit> binary = new ArrayList<>();
        String s = "";
        for (Digit d : binary) {
            s = s + d.toString();
        }

        return s;
    }
}

You are using different binary list for each operation, try this:

public class DNumber {

ArrayList<Digit> binary = new ArrayList<Digit>();

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

public DNumber(int val) {
    int num = val;
    while (num > 0) {
        Digit bin = new Digit(num % 2);
        num /= 2;
        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;
}

}

In each of your methods you create a new ArrayList<Digit> . So, in the last one you are trying to iterate in an empty ArrayList.

Delete all the ArrayList<Digit> binary = new ArrayList<>(); inside your methods:

public class DNumber {

    ArrayList<Digit> binary = new ArrayList<Digit>();

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

    public DNumber(int val) {
        int num = val;
        while (num > 0) {
            Digit bin = new Digit(num % 2);
            num /= 2;
            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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM