简体   繁体   English

如何检查ArrayList中一个条目的一个字符串和一个字符串属性(在多个属性中)的相等性?

[英]How do I check the equality of a String and one String-attribute (out of many) of an entry in an ArrayList?

I'm trying to create a registry in form of an ArrayList which shall save all kinds of books I've read. 我正在尝试以ArrayList的形式创建注册表,该注册表将保存我已阅读的所有书籍。 The program has only two classes. 该程序只有两个类。 One as a template for the entries (Name: Entry) and one which manages the entries in the ArrayList (Name: Registry). 一种作为条目的模板(名称:Entry),另一种用于管理ArrayList中的条目(名称:Registry)。

These are the attributes of Entry: 这些是Entry的属性:

private final String title;
private final String mangaka;
private final String year;
private final String genre;
private final String volumes;
private String completelyScanlated;
private String licensedInGerman;
private String read; 
private String comments; 

and thus has this constructor: 因此具有以下构造函数:

public Entry(final String title, final String mangaka, final String year, final String genre, final String volumes, 
String completelyScanlated, String licencedInGerman, String read, String comment)
{
    this.title = title;
    this.mangaka = mangaka;
    this.year = year;
    this.genre = genre;
    this.volumes = volumes;
    this.completelyScanlated = completelyScanlated;
    this.licensedInGerman = licensedInGerman;
    this.read = read;
    this.comments = comments;
}

The class "Registry" has only one attribute: “注册表”类只有一个属性:

ArrayList<Entry> entries = new ArrayList<Entry>();

The user creates an entry via 'Scanner' therefore by typing Strings into the console. 用户通过“扫描仪”创建一个条目,因此在控制台中输入字符串。 The created object is saved in the ArrayList via: 创建的对象通过以下方式保存在ArrayList中:

Entry object = new Entry(title, mangaka, year, genre, volumes, completelyScanlated, licensedInGerman, read, comments);
    entries.add(object);

Now I want to check whether a String (which is also created with a console input) is equal to the attribute "title". 现在,我要检查一个字符串(也通过控制台输入创建的字符串)是否等于属性“ title”。 I could check the equality of an input with the method ".contains()" but this method would compare all attributes. 我可以使用方法“ .contains()”检查输入是否相等,但是此方法将比较所有属性。 Is there a way to check only one attribute? 有没有办法只检查一个属性?

Here is the non-working code: 这是无效的代码:

public void findEntry()
{
    Scanner input = new Scanner(System.in);

    System.out.println("Which title do you want to search for?");
    String searchedEntry = input.nextLine();
    System.out.println();
    if (entries.contains(searchedEntry)) {
        int x = entries.indexOf(searchedEntry);
        entries.get(x);
        //Entry.showDetails();
    }
}

The result is to given out via the console (that code is working though). 结果是通过控制台给出的(尽管代码正在运行)。

Thanks in advance 提前致谢

I assume you have a getter for your Title in your Entry class. 我假设您在Entry类中有一个标题的getter。 Something like getTitle(). 类似于getTitle()。 Then iterate over all entries and check if its title contains the search string. 然后遍历所有条目,并检查其标题是否包含搜索字符串。

public void findEntry(){
    Scanner input = new Scanner(System.in);

    System.out.println("Which title do you want to search for?");
    String searchedEntry = input.nextLine();
    System.out.println();
    for(Entry entry : entries){
        if(entry.getTitle().contains(input))
            entry.showDetails();   // or Whatever
    }
}

Even better whould it be to convert the title and the search string to lower cases befor. 最好是将标题和搜索字符串转换为小写形式。

if(entry.getTitle().toLowerCase().contains(input.toLowerCase()))

So you can search for "lord of THE rings" and will find "The Lord of the Rings: The Fellowship of the Ring". 因此,您可以搜索“指环王”,然后找到“指环王:指环王”。

Instead of 代替

if (entries.contains(searchedEntry)) {
    int x = entries.indexOf(searchedEntry);
    entries.get(x);
    //Entry.showDetails();
}

You can iterate through entries. 您可以遍历条目。 Something like this: 像这样:

for(Entry entry: entries) {
    if(entry.getTitle().equals(searchedEntry)) {
        // Do whatever it has to do.
    }
}

If title doesn't need to match the case, you can use: 如果标题不需要与大小写匹配,则可以使用:

if(entry.getTitle().equalsIgnoreCase(searchedEntry))

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

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