无法从int []转换为int?

时间:2015-12-09 15:42:16

标签: java arrays arraylist int

我一般都是编程新手,我一直在尝试用java为我的comp sci课程构建一个系统,我在eclipse中得到错误“无法从int []转换为int”。

public static ArrayList<Book> CreateBooksArrayList(){
    int[] id = {1,2,3,4,5};
    String[] bookTitle = {"book1", "book2", "book3", "book4", "book5"};
    String[] authorName = {"author1", "author2", "author3", "author4", "author5"};
    double[] bookReleaseYear = {123,456,789,987,654};
    int[] numOnLoan = {1,2,2,3,4};
    int[] numInStock = {5,5,5,5,5};


    int bID = -1;
    String bTitle = null;
    String aName = null;
    double bRelease = -1;
    int nLoan = -1;
    int nStock = -1;

    ArrayList<Book> Books = new ArrayList<Book>();
    for (int i = 0; i<6; i++){
        bID = id[i];
        bTitle = bookTitle[i];
        aName = authorName[i];
        bRelease = bookReleaseYear[i];
        nLoan = numOnLoan;
        nStock = numInStock;
        Books.add(new Book(bID, bTitle, aName, bRelease, nLoan, nStock));

我试过寻找答案,但我似乎无法找到这个确切问题的链接。在此先感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

您忘记了for循环中[i]numOnLoan的{​​{1}}部分。更改它们以包括numInStock

[i]

答案 1 :(得分:0)

替换

nLoan = numOnLoan;
nStock = numInStock;

with:

nLoan = numOnLoan[i];
nStock = numInStock[i];

答案 2 :(得分:0)

尝试更改这些行:

#include <exception>

class Exception :
    public std::exception {
private:
    static const std::string MESSAGE = "Exception";

protected:
    std::string composedMessage;

public:
    Exception() :
    composedMessage(this->MESSAGE) {
    }

    virtual const char* what() const throw() {
        return this->composedMessage.c_str();
    }
};

class ShoulderROMException :
    public Exception {
private:
    static const std::string MESSAGE = "ShoulderROM exception";

public:
    ShoulderROMException() {
        this->appendMessage(this->MESSAGE);
    }

    virtual void appendMessage(std::string message) {
        this->composedMessage += " -> ";
        this->composedMessage += message;
    }
};

class KinectInitFailedException :
    public ShoulderROMException {
private:
    static const std::string MESSAGE = "Kinect initialization failed.";

public:
    KinectInitFailedException() {
        this->appendMessage(this->MESSAGE);
    }
};

通过这些,您忘记了循环的索引 nLoan = numOnLoan; nStock = numInStock; 以从数组中检索值:

i