如何将对象数组从一种方法转换为另一种方法并返回一个新对象?

时间:2019-02-04 22:43:39

标签: java arrays methods

我需要使用主要方法中的数组,并转移到我的 getBook方法中, 然后将新创建的对象返回到main方法中的数组。

我决定将阵列转移到下一个方法,方法与使用扫描仪相同,直到扫描仪出错时需要调用该方法为止。

public class BookShopApplication 
{

    public static void main(String[] args) 
    {
        Scanner kybd = new Scanner (System.in);
        Book [] books = new Book [10];

        for (int i = 0; i > books.length; i++) 
        {
            books[i] = getBook(kybd, Book books[])
        }

    }

    public static Book[] getBook(Scanner kybd, Book books[]) 
    {


        System.out.println("What is the title of the next book?");
        String readTitle = kybd.nextLine();

        System.out.println("What is thr title of thje next book?");
        String readAuthor = kybd.nextLine();

        if (readAuthor == null)
        {
            for (int i = 0; i < books.length; i++)
            {
                books[i] = new Book();
                books[i].Book(readTitle);
            }

        }
        else 
        {
             for (int i = 0; i < books.length; i++)
            {
                books[i] = new Book();

                books[i].Book(readAuthor, readTitle);


            }

        }
    return books;
    }
}

我需要的结果是将书返回数组并存储。

3 个答案:

答案 0 :(得分:2)

假设您的Book类看起来与此类似:

class Book
{
    private String title;
    private String author;

    public void setTitle(String title)
    {
        this.title = title;
    }

    public void setAuthor(String author)
    {
        this.author = author;
    }
}

更有意义的是,您尝试分别实例化书籍并将它们存储在Book对象的数组中。

public static void main(String[] args) {

    Scanner kybd = new Scanner (System.in);
    Book [] books = new Book [10];

    // < is needed not >
    for (int i = 0; i < books.length; i++) 
    {
        // book[] is empty, it has an index of 10 with no nodes
        // You need to create the instance to pass into the method
        books[i] = getBook(kybd, new Book());
    }
}

// This should only take the book it is working with
public static Book getBook(Scanner kybd, Book book)
{
    System.out.println("What is the title of the next book?");
    String readTitle = kybd.nextLine();

    System.out.println("What is the title of the next book?");
    String readAuthor = kybd.nextLine();

    if (!readAuthor.isEmpty())
    {
        book.setTitle(readTitle);
    }

    if(!readTitle.isEmpty())
    {
        book.setAuthor(readAuthor);
    }

    return book;
}

您还有很多语法错误,而且-使用.isEmpty()方法而不是使用=== null

更新:您实际上从未在循环中创建Book的实例。另外,for (int i = 0; i > books.length; i++)-应该是<而不是>

此代码现在应该可以工作。

答案 1 :(得分:0)

好吧,您确实有一些语法错误:

首先,在您的for循环中,books[i] = getBook(kybd, Book books[])应替换为books[i] = getBook(kybd, books)(要将变量作为参数传递给方法,只需使用其名称即可!)

第二,在方法签名中,将public static Book[] getBook(Scanner kybd, Book books[])替换为public static Book[] getBook(Scanner kybd, Book[] books) (请注意,它是Book[] books,而不是Book books[])。

希望这会有所帮助!

而且,正如Jaquarh所说,您缺少分号!

编辑:此外,您的方法getBooks应该返回一本书,而不是Jaquarh指出的数组。否则,线 books[i] = getBook(kybd, Book books[])将崩溃。

答案 2 :(得分:-1)

for (int i = 0; i > books.length; i++) 
    {
        books[i] = getBook(kybd,books);
    }

应该做到这一点,而不会引起错误

这也是无效的:

  for (int i = 0; i < books.length; i++)
        {
            books[i] = new Book();
            **books[i].Book(readTitle);**
        }

    }
    else 
    {
         for (int i = 0; i < books.length; i++)
        {
            books[i] = new Book();

            **books[i].Book(readAuthor, readTitle);**

已编辑

相关问题