简体   繁体   English

如何遍历数组列表并打印包含单词的索引?

[英]How to iterate through a arraylist and print the index of the containing word?

I want to know how to iterate through a arraylist and print out all the index's containing a number that the user put in the scanner.我想知道如何遍历数组列表并打印出包含用户放入扫描仪中的数字的所有索引。 If the number is not in the scanner then it would print out value is not in the list.如果数字不在扫描仪中,那么它会打印出不在列表中的值。 I have two classes in my program.我的程序中有两个类。

Below is my Entertainment Class:以下是我的娱乐课:

         public class Real_Entertainment 
      {
            String BookName; 
            String Author; 
            double Rating; 
            int Purchases;


public Real_Entertainment(String BookName, String Author, double Rating, int Purchases)
{
    this.BookName = BookName;
    this.Author = Author;
    this.Rating = Rating;
    this.Purchases = Purchases;
}
public void setBookName(String BookName)
{
    this.BookName = BookName;
}
public void setAuthor (String Author )
{
    this.Author = Author;
}
public void setRating (double Rating)
{
    this.Rating = Rating;
}
public void setPurchased (int Purchases)
{
    this.Purchases = Purchases;
}


    }

Below is the main class that contains the arraylist:下面是包含数组列表的主类:

    import java.util.Scanner;
    import java.util.ArrayList;
    public class Real_Main 
    {




        public static void main(String[] args) 
        {


    ArrayList<Real_Entertainment> print = new ArrayList<Real_Entertainment>();
    Real_Entertainment Book1 = new Real_Entertainment("The Nickel Boys ", "Colson Whitehead", 4.3, 500000);
    Real_Entertainment Book2 = new Real_Entertainment("Olive, Again", "Elizabeth Strout", 6, 321000);
    Real_Entertainment Book3 = new Real_Entertainment("Gingerbread", "Helen Oyeyemi", 2, 681000);
    Real_Entertainment Book4 = new Real_Entertainment("On Earth We're Briefly Gorgeous", "Ocean Vuong", 2, 421000);

    print.add(Book1);
    print.add(Book2);
    print.add(Book3);
    print.add(Book4);



 findPrimitiveValue(print);


}

public static void findPrimitiveValue(ArrayList<Real_Entertainment> print)
{
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a Numeric Value:" );
        int input = scanner.nextInt();
        boolean foundInfo = false;
        for(int i = 0; i < print.size(); i++)
        {
            if(print.get(i).() == input || print.get(i).() == input)
            {
                System.out.println("This is the information found: " );
                System.out.println()
            }
        }

}

   }

I have tried to set the value of the purchases = to the value that the user inputted I kept getting an error.我试图将购买的值设置为用户输入的值,但我一直收到错误消息。

I refactored your code for我重构了你的代码

  • class, field and parameter names according to conventions,根据约定的类、字段和参数名称,
  • turning your Book class into a Java Bean which also has a toString() method for easy printing,将你的Book类变成一个 Java Bean,它还有一个toString()方法以便于打印,
  • using less cryptic names in general,一般使用不那么神秘的名字,
  • using Java streams for nicer collection handling.使用 Java 流来更好地处理集合。

I also added an example of how you can eg search for an exact match in number of purchases for a book.我还添加了一个示例,说明如何搜索一本书的购买数量完全匹配。 I also added an additional book with the same number of purchases so we can see more than one search result.我还添加了另一本书,购买次数相同,因此我们可以看到多个搜索结果。

Data class:数据类:

package de.scrum_master.stackoverflow.q60461642;

public class Book {
  private String name;
  private String author;
  private double rating;
  private int purchases;

  public Book(String name, String author, double rating, int purchases) {
    this.name = name;
    this.author = author;
    this.rating = rating;
    this.purchases = purchases;
  }

  @Override
  public String toString() {
    return "Book{" +
      "name='" + name + '\'' +
      ", author='" + author + '\'' +
      ", rating=" + rating +
      ", purchases=" + purchases +
      '}';
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAuthor() {
    return author;
  }

  public void setAuthor(String author) {
    this.author = author;
  }

  public double getRating() {
    return rating;
  }

  public void setRating(double rating) {
    this.rating = rating;
  }

  public int getPurchases() {
    return purchases;
  }

  public void setPurchases(int purchases) {
    this.purchases = purchases;
  }
}

Driver application:驱动程序应用:

package de.scrum_master.stackoverflow.q60461642;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Application {
  private static List<Book> books = Arrays.asList(
    new Book("The Nickel Boys", "Colson Whitehead", 4.3, 500000),
    new Book("Olive, Again", "Elizabeth Strout", 6, 321000),
    new Book("Gingerbread", "Helen Oyeyemi", 2, 681000),
    new Book("On Earth We're Briefly Gorgeous", "Ocean Vuong", 2, 421000),
    new Book("Perfume", "Patrick Süskind", 6, 500000)
  );

  public static void main(String[] args) {
    printBooks();
    findBooksByNumberOfPurchases();
  }

  private static void printBooks() {
    System.out.println("Books list:");
    books.stream().forEach(System.out::println);
    System.out.println("\n");
  }

  private static void findBooksByNumberOfPurchases() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the number of purchases to search for:");
    int input = scanner.nextInt();
    books.stream()
      .filter(book -> book.getPurchases() == input)
      .forEach(System.out::println);
  }
}

Console log:控制台日志:

Books list:
Book{name='The Nickel Boys', author='Colson Whitehead', rating=4.3, purchases=500000}
Book{name='Olive, Again', author='Elizabeth Strout', rating=6.0, purchases=321000}
Book{name='Gingerbread', author='Helen Oyeyemi', rating=2.0, purchases=681000}
Book{name='On Earth We're Briefly Gorgeous', author='Ocean Vuong', rating=2.0, purchases=421000}
Book{name='Perfume', author='Patrick Süskind', rating=6.0, purchases=500000}


Enter the number of purchases to search for:
500000
Book{name='The Nickel Boys', author='Colson Whitehead', rating=4.3, purchases=500000}
Book{name='Perfume', author='Patrick Süskind', rating=6.0, purchases=500000}

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

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