我如何将大小为 i 的数组列表的元素显示到大小写切换系统中?

How would I display the elements of an arraylist of size i, into a case switch system?

我一直在研究一个将书籍对象存储到图书馆的程序,但我已经停滞不前了。

我需要从我的数组列表 BookList 中打印出大小未知的所有 Book 对象(由于可能包括将新书添加到系统中的功能),到大小写切换系统中,这样您就可以 select 每个选项并编辑详细信息。

我是否需要找到 ArrayList.length 并使用它来确定 switch 语句选项的数量?

我的代码如下

import java.util.Scanner;
import java.util.ArrayList;

public class Library_Tester {


public static void main (String[] args){





    System.out.println(" ===========================================");
    System.out.println("|  ==  ==  ===== ==    ==       ====        |");
    System.out.println("|  ==  ==  ==    ==    ==     =      =      |");
    System.out.println("|  ======  ====  ==    ==    =        =     |"); 
    System.out.println("|  ==  ==  ==    ==    ==     =      =      |");
    System.out.println("|  ==  ==  ===== ===== =====    ====        |");
    System.out.println(" ===========================================");



        System.out.println(" =======================================");
        System.out.println("|        Library Systems Inc            |");
        System.out.println(" =======================================");
        System.out.println("| Options:                              |");
        System.out.println("|       1. Add a book                   |");
        System.out.println("|       2. Edit a book's details        |");
        System.out.println("|       3. Delete a book                |");
        System.out.println("|       4. Loan a book                  |");
        System.out.println("|       5. Return a book                |");
        System.out.println("|       6. Exit the Program             |");
        System.out.println("|       7. Display all books in library |");
        System.out.println("|                                       |");
        System.out.println("|  *Type a number to make a selection*  |");
        System.out.println(" =======================================");
        System.out.println("");
        System.out.print("Selection: ");

        Book a = new Book();

        a.setTitle("Random ");
        a.setAuthor("Craig Robertson");
        a.setBookID("1847398812");
        a.setonLoan(false);
        a.setNumberofLoans(3);

        Book b = new Book();

        b.setTitle("The Last Refuge");
        b.setAuthor("Craig Robertson");
        b.setBookID("1471127753");
        b.setonLoan(false);
        b.setNumberofLoans(2);

        Book c = new Book();

        c.setTitle("The Bird That Did Not Sing");
        c.setAuthor("Alex Gray");
        c.setBookID("0751548278");
        c.setonLoan(true);
        c.setNumberofLoans(7);

        ArrayList<Book> BookList = new ArrayList<Book>();
        BookList.add(a);
        BookList.add(b);
        BookList.add(c);





    Scanner SC = new Scanner(System.in);

    int Choice1;

       Choice1 = SC.nextInt();

       SC.close();


        switch (Choice1) {

        case 1:

            Scanner JK = new Scanner(System.in);

          System.out.println("'Add a book' selected");
          System.out.println(" ");


          break;

        case 2:
            System.out.println("'Edit a book's details' selected");
            System.out.println("Which Book would you like to edit?");
            System.out.println("");
            break;

        case 3:
            System.out.println("'Delete a book' selected");
            break;

        case 4:
              System.out.println("'Loan a book' selected");
              break;

        case 5:
              System.out.println("'Return a book' selected");
              break;

        case 6:
              System.out.println("Goodbye!");
              System.exit(0);

              break;

        case 7:
              System.out.println("Displaying Books");
              System.out.println("");
              Book.showBooks(BookList);
              System.out.println("");

              break;      

        default:
          System.out.println("Invalid selection. Try again");


        }
      }
      }

可根据要求将其他编码编辑到此问题中。

一个额外的问题是:如何在这之后编辑数组中 Book 对象的状态?

提前致谢。

只需使用 foreach 语句

for (Book book : bookList){
    System.out.println(book.getTitle());
}

想法是,对于您在 case 语句中调用的每个方法,您的程序将遍历书籍列表,以显示它们或允许用户修改它们

我认为这与您尝试做的恰恰相反,我的理解是遍历列表并显示每本书的所有选项。反其道而行之

遗憾的是,switch cases 只接受编译时已知常量的值。 通常,如果编译器在您的 switch 语句中为每个 case 找到合适的常量,它们将为您构建一个 jump table 用于 switch case。 请注意,switch 仅适用于整数基本类型和枚举,因此如果您使用其他对象类型,则需要使用 if/else 语句。

为什么要使用开关盒?

获取用户输入,例如 bookId 并在列表中搜索该对象,然后编辑该对象的详细信息。

步骤; 1.选择编辑书籍 2. 求书号 3. 从数组列表

中获取带有 id 的书
List<Book> books = new ArrayList<Book>();

// Populate list

private Book getBookById(int id) {
    Iterator<Book> obj = books.iterator();
    while(obj.hasNext()) {
        Book book = obj.next();
        if(book.id == id)
            return book;
    }
    return null;
}