Java ArrayList打印多次

Java ArrayList打印多次,java,hashmap,Java,Hashmap,我的问题是,当我运行这段代码时,ArrayList中的所有内容都会打印大约3次 以下是您需要查看的代码: static ArrayList<String> list = new ArrayList<String>(); static HashMap<String, Character> books = new HashMap<String, Character>(); public static void main(String[] args)

我的问题是,当我运行这段代码时,ArrayList中的所有内容都会打印大约3次

以下是您需要查看的代码:

static ArrayList<String> list = new ArrayList<String>();
static HashMap<String, Character> books = new HashMap<String, 
Character>();

public static void main(String[] args){
//      Title, Section
books.put("Harry Potter and the blank blank blank", 'A');
books.put("Harry Potter and the blank", 'Z');
books.put("Harry Potter ", 'Z');
books.put("Harry Potter and the blank blank", 'L');
findBook("Harry");
}

public static void findBook(String title){
list.clear();
for (String key : books.keySet()) {
  if(key.equalsIgnoreCase(title)){
    System.out.println(books.get(key));
  }
  else if (key.startsWith(title)) {
    list.add(key);
  }else System.out.println("Book not found");
    for(String bookTitle : list){
      System.out.println("Book: " + bookTitle);
  }
}
static ArrayList list=new ArrayList();
静态HashMap books=newHashMap();
公共静态void main(字符串[]args){
//标题,章节
书(放“哈利·波特和空白空白”,“A”);
books.put(“哈利波特与空白”,“Z”);
书。放(“哈利波特”,“Z”);
书(放“哈利·波特和空白的空白”,“L”);
findBook(“哈利”);
}
公共静态void findBook(字符串标题){
list.clear();
for(字符串键:books.keySet()){
if(关键同等信号案例(标题)){
System.out.println(books.get(key));
}
else if(钥匙起动带(标题)){
列表。添加(键);
}else System.out.println(“找不到书”);
for(字符串书名:列表){
System.out.println(“书:”+书名);
}
}
请帮忙,非常感谢!
-S1ant

您已将打印书籍的
for
循环保留在找到的循环中,因此您将获得多个打印。将打印的for循环移出该循环,如下所示:

static ArrayList<String> list = new ArrayList<String>();
static HashMap<String, Character> books = new HashMap<String, 
Character>();

public static void main(String[] args){
//      Title, Section
books.put("Harry Potter and the blank blank blank", 'A');
books.put("Harry Potter and the blank", 'Z');
books.put("Harry Potter ", 'Z');
books.put("Harry Potter and the blank blank", 'L');
findBook("Harry");
}

public static void findBook(String title){
list.clear();
for (String key : books.keySet()) {
  if(key.equalsIgnoreCase(title)){
    System.out.println(books.get(key));
  }
  else if (key.startsWith(title)) {
    list.add(key);
  }else 
    System.out.println("Book not found");
  }
}

// Move this out of the above loop
for(String bookTitle : list){
  System.out.println("Book: " + bookTitle);
static ArrayList list=new ArrayList();
静态HashMap books=newHashMap();
公共静态void main(字符串[]args){
//标题,章节
书(放“哈利·波特和空白空白”,“A”);
books.put(“哈利波特与空白”,“Z”);
书。放(“哈利波特”,“Z”);
书(放“哈利·波特和空白的空白”,“L”);
findBook(“哈利”);
}
公共静态void findBook(字符串标题){
list.clear();
for(字符串键:books.keySet()){
if(关键同等信号案例(标题)){
System.out.println(books.get(key));
}
else if(钥匙起动带(标题)){
列表。添加(键);
}否则
System.out.println(“找不到书”);
}
}
//将其移出上述循环
for(字符串书名:列表){
System.out.println(“书:”+书名);

每次您试图查找一本书时,都会执行下面的for循环。因此,您会看到多个打印

for (String bookTitle : list) {
    System.out.println("Book: " + bookTitle);
}

您可以将上面的for循环移动到main方法中,并在findBook方法调用之后立即执行。

好的,但是如果我想让它更具可定制性呢?就像我想添加一个方法来添加书籍一样,我不会在代码中调用findBook方法,我会从控制台调用它。我也不会将书籍硬编码,我会将它们添加到从控制台。