Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何向数组中添加新条目?_Java - Fatal编程技术网

Java 如何向数组中添加新条目?

Java 如何向数组中添加新条目?,java,Java,对于这个项目,我只能从堆中移除最上面的一本,而不能从另一本下面移除一本书。同样,我不能在另一本书下面加一本书。我只能将另一本书放在书堆的顶部,才能在书堆中添加另一本书。在我的代码中,我从书堆中删除了E本书,但现在我想添加一本不同的书。如何在myBooks中添加新书(book:F)并打印列表?有一个链接指向我当前输出的屏幕截图 公共类驱动程序 { 公共静态void main(字符串[]args) { 书籍[]我的书籍={新书(“A”)、新书(“B”)、新书(“C”)、新书(“D”)、新书(“E

对于这个项目,我只能从堆中移除最上面的一本,而不能从另一本下面移除一本书。同样,我不能在另一本书下面加一本书。我只能将另一本书放在书堆的顶部,才能在书堆中添加另一本书。在我的代码中,我从书堆中删除了E本书,但现在我想添加一本不同的书。如何在myBooks中添加新书(book:F)并打印列表?有一个链接指向我当前输出的屏幕截图

公共类驱动程序
{
公共静态void main(字符串[]args)
{
书籍[]我的书籍={新书(“A”)、新书(“B”)、新书(“C”)、新书(“D”)、新书(“E”)};
PileOfBooks接口书堆=新的PileOfBooks();
System.out.println(“书堆里有书吗?”+bookspiles.isEmpty());
for(int index=0;index
有了书堆,你就不再需要一堆书了,你会按照你所期望的方式工作

公共类驱动程序{
公共静态void main(字符串[]args){
堆栈书堆=数组.asList(新书(“A”)、新书(“B”)、新书(“C”)、新书(“D”)、新书(“E”).stream()
.collect(收集器.toCollection(Stack::new));
System.out.println(“书堆里有书吗?”+!bookspiles.isEmpty());
System.out.println(“\n书堆中的全部书籍:”);
bookspiles.stream().forEach(System.out::println);
System.out.println(“\n删除最后一本书:”);
书堆;
bookspiles.stream().forEach(System.out::println);
System.out.println(“\n在书堆顶部添加新书:”;
书堆。推(新书(“F”);
bookspiles.stream().forEach(System.out::println);
}
}
如果PileOfBooks已经编写,并且按照您的描述工作,那么您只需要向PileOfBooks添加一本书,就像您在开始添加书时所做的那样

书堆。添加(新书(“F”);

这很简单。您可以使用ArrayList存储所有书籍,java为ArrayList提供了非常好的内置方法。您可以执行以下操作:

public class Driver
{
    public static void main(String[] args)
    {
        Book[] myBooks = { new Book("A"), new Book("B"), new Book("C"), new Book("D"), new Book("E")};
        ArrayList<Book> bookPiles = new ArrayList<Book>();
        System.out.println("Are there any books in the pile? " + bookPiles.isEmpty());

        for (int index = 0; index < myBooks.size(); index++)
        {
            Book nextItem = myBooks[index];
            bookPiles.add(nextItem);//populating ArrayList
        } // end for

        System.out.println("\nTotal books in the pile:");

        for (int index = 0; index < myBooks.size(); index++) 
        {
            System.out.println(bookPiles.get(index));
        } // end for

        System.out.println("\nRemoving the last book:");
        bookPiles.remove(bookPiles.size()-1); //removing the last element from the bookPile ArrayList
        //Object[] arr = (bookPiles.toArray()); //No need for this line

        for (int index = 0; index < bookPiles.size(); index++) 
        {
            System.out.println(bookPiles.get(index));
        } // end for

        System.out.println("\nAdding new book on top of the pile:");
         // you can write the code for adding at the top of the pile as follows:

        bookPiles.add(0,new Book("E"));//0 is the first position in the ArrayList where we want to add data and all the elements will be shifted automatically.
        //the above line will add the new book at the top of the pile.
        for (int index = 0; index < bookPiles.size(); index++) 
        {
            System.out.println(bookPiles.get(index)); //print all the books along with the new one
        } 


    }
}
公共类驱动程序
{
公共静态void main(字符串[]args)
{
书籍[]我的书籍={新书(“A”)、新书(“B”)、新书(“C”)、新书(“D”)、新书(“E”)};
ArrayList bookPiles=新的ArrayList();
System.out.println(“书堆里有书吗?”+bookspiles.isEmpty());
对于(int index=0;index

希望这对您有所帮助。

如果没有看到
PileOfBooks
我们甚至猜不出
remove
应该做什么,或者eHeather有一个
add
method也
System.out.println(“书堆里有书吗?”+!bookspiles.isEmpty())
public class Driver
{
    public static void main(String[] args)
    {
        Book[] myBooks = { new Book("A"), new Book("B"), new Book("C"), new Book("D"), new Book("E")};
        ArrayList<Book> bookPiles = new ArrayList<Book>();
        System.out.println("Are there any books in the pile? " + bookPiles.isEmpty());

        for (int index = 0; index < myBooks.size(); index++)
        {
            Book nextItem = myBooks[index];
            bookPiles.add(nextItem);//populating ArrayList
        } // end for

        System.out.println("\nTotal books in the pile:");

        for (int index = 0; index < myBooks.size(); index++) 
        {
            System.out.println(bookPiles.get(index));
        } // end for

        System.out.println("\nRemoving the last book:");
        bookPiles.remove(bookPiles.size()-1); //removing the last element from the bookPile ArrayList
        //Object[] arr = (bookPiles.toArray()); //No need for this line

        for (int index = 0; index < bookPiles.size(); index++) 
        {
            System.out.println(bookPiles.get(index));
        } // end for

        System.out.println("\nAdding new book on top of the pile:");
         // you can write the code for adding at the top of the pile as follows:

        bookPiles.add(0,new Book("E"));//0 is the first position in the ArrayList where we want to add data and all the elements will be shifted automatically.
        //the above line will add the new book at the top of the pile.
        for (int index = 0; index < bookPiles.size(); index++) 
        {
            System.out.println(bookPiles.get(index)); //print all the books along with the new one
        } 


    }
}