简体   繁体   English

我如何在我的 java 代码中使用这个 copyOf 数组 function?

[英]How do i use this copyOf array function in my java code?

Im trying to create a new instance in my book array, i've researched on the copyof method and thats supposedly how you do it, however, when I run the code that supposedly does it, i dont get any error or anything but it just doesnt show up?我试图在我的书数组中创建一个新实例,我研究了 copyof 方法,这就是你应该如何做的,但是,当我运行应该这样做的代码时,我没有得到任何错误或任何东西,但它只是没有出现? the output shows the other 3 books but not the added harry potter one. output 显示了其他 3 本书,但没有显示添加的哈利波特一本书。

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {

        Book book1 = new Book("The Alchemist", "Paulo Coelho", "HarperCollins", "Adventure");
        Book book2 = new Book("The Great Gatsby", "F. Scott Fitzgerald", "Charles Scribner\'s Sons", "Fiction");
        Book book3 = new Book("The Catcher in the Rye", "J. D. Salinger", "Little, Brown and Company", "Fiction");

// Create an instance of the class Author

        Author author1 = new Author("Paulo", "Coelho");
        Author author2 = new Author("F. Scott", "Fitzgerald");
        Author author3 = new Author("J. D.", "Salinger");

// Create an instance of the class Publisher

        Publisher publisher1 = new Publisher("HarperCollins");
        Publisher publisher2 = new Publisher("Charles Scribner\'s Sons");
        Publisher publisher3 = new Publisher("Little, Brown and Company");

        String[] authors = {author1.toString(), author2.toString(), author3.toString()};
        String[] publishers = {publisher1.toString(), publisher2.toString(), publisher3.toString()};
        String[] books = {book1.toString(), book2.toString(), book3.toString()};

        addBook(authors, publishers, books, "J. K. Rowling", "Bloomsbury", "Harry Potter and the Philosopher\'s Stone", "trash");

        //System.out.println(Arrays.toString(books));
        //displayBooks(books);
       // displayAuthors(authors);
        //displayPublishers(publishers);

    }
    public static void addBook(String[] authors, String[] publishers, String[] books, String author, String publisher, String book, String genre){
        authors = Arrays.copyOf(authors, authors.length + 1);
        authors[authors.length - 1] = author;

        publishers = Arrays.copyOf(publishers, publishers.length + 1);
        publishers[publishers.length - 1] = publisher;

        books = Arrays.copyOf(books, books.length + 1);
        books[books.length - 1] = book;
    }

    public static void displayAuthors(String[] authors){
        for(String author : authors){
            System.out.println(author);
        }

    }
    public static void displayPublishers(String[] publishers){
        for(String publisher : publishers){
            System.out.println(publisher);
        }
    }
    public static void displayBooks(String[] books) {
        for (String book : books) {
            System.out.println(book);

        }
    }
}

Constructor for book:本书的构造函数:

public class Book {

    String title;
    String author;
    String publisher;
    String genre;

    public Book(String title,String author,String publisher,String genre) {
        this.title = title;
        this.author = author;
        this.publisher = publisher;
        this.genre = genre;
    }
    public String toString() {
        return this.title + " | " + this.author + " | " + this.publisher + " | " + this.genre;
    }
}

I've tried System.out.println(Arrays.toString(books));我试过 System.out.println(Arrays.toString(books)); There is no error, so maybe it does happen but when i go to print the array itself it doesnt show up (I'd like all 4 books to show instead of the original 3)没有错误,所以也许它确实发生了但是当我 go 打印数组本身时它没有显示(我希望显示所有 4 本书而不是原来的 3 本书)

In your addBook method:在您的addBook方法中:

 authors = Arrays.copyOf(authors, authors.length + 1); authors[authors.length - 1] = author;

authors is your own local variable. authors是你自己的局部变量。 You create a new array here (with copyOf ), and assign it to this local copy.您在此处创建一个新数组(使用copyOf ),并将其分配给此本地副本。 You then change something in this array you just made, and then your method ends.然后您更改刚刚创建的数组中的某些内容,然后您的方法结束。 With it, your local variable poofs out of existence, and with that, no code has any reference to this copied array.有了它,您的局部变量就消失了,并且没有代码引用这个复制的数组。 The garbage collector eventually removes it.垃圾收集器最终将其删除。

The entire method therefore does nothing.因此整个方法什么都不做。

Generally, don't have static anything when you start out (other than your main , which should contain just one line: new MyClass().go() , or possibly new MyClass().go(args) if you care about the args; no further use of static allowed until you're a bit further along in your java career.一般来说,当你开始时不要有任何static任何东西(除了你的main ,它应该只包含一行: new MyClass().go() new MyClass().go(args)或者如果你关心args;在您的 java 职业生涯中进一步发展之前,不允许进一步使用static

If you want things to survive, they have to be fields.如果你想让东西生存,它们就必须是田地。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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