【问题标题】:How can I take an Array of objects from one method to another and return a new object? [closed]如何将对象数组从一种方法转移到另一种方法并返回一个新对象? [关闭]
【发布时间】:2019-02-04 22:43:39
【问题描述】:

我需要使用 ma​​in 方法 中的 array 并将其转移 到我的 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;
    }
}

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

【问题讨论】:

  • 你的错误是什么?
  • @Jaquarh 第 14 行不是声明
  • public static Book[] getBook(Scanner kybd, Book[] books) - books 是变量,Book[] 是数据类型。在调用该方法后,您还缺少 ; - 您有语法错误

标签: java arrays methods


【解决方案1】:

假设您的 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 &gt; books.length; i++) - 应该是 &lt; 而不是 &gt;

这段代码现在应该可以工作了。

【讨论】:

  • 谢谢 :) 现在一切正常
  • 别担心,希望对您有所帮助
【解决方案2】:

嗯,你确实有一些语法错误:

首先,在您的 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 所说,您缺少分号!

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

【讨论】:

  • 感谢您的回答:),我已经编辑了代码,但现在我收到错误“不兼容的类型 Book[] 无法转换为 Book
  • @ShaneJones 你在哪一行得到这个?
  • 从 getBook 你返回 Book[] 并且在第 11 行你正在查看 Book 因为在 7 你声明> Book [] books = new Book [10];
  • 第 14 行,books[i] = getBook(kybd, books);
  • @ShaneJones 哦,好吧,你的方法 getBooks 返回一系列书籍,而不仅仅是一本书!
【解决方案3】:
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);**

已编辑

【讨论】:

  • 它仍然有错误:.class expected, ;预期,找不到符号,意外类型
  • 因为getBook是一个方法,所以需要以;结束语句
  • 这不会编译,因为book[] 作为参考无效。
  • 即使是 ;它仍然与不兼容类型的错误 Book[] 无法转换为 Book
猜你喜欢
  • 2021-08-11
  • 2014-10-18
  • 2017-03-19
  • 1970-01-01
  • 2021-04-23
  • 2023-04-09
  • 2020-07-06
  • 1970-01-01
  • 2016-02-11
相关资源
最近更新 更多