Java:将 ArrayList 项与用户输入字符串进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25087878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:36:32  来源:igfitidea点击:

Java: Comparing ArrayList item to User Input String

javastringarraylist

提问by Josh

I have seen other threads on this, but I still don't understand the best approach. Issue is as started: One can't compare user input that is a String to an arrayList item (object). My List will be over 40 items.

我已经看过其他线程,但我仍然不明白最好的方法。问题开始时:无法将作为字符串的用户输入与 arrayList 项目(对象)进行比较。我的清单将超过 40 项。

So when I try to compare classesList.get(0)to the user input mage. It won't work.

因此,当我尝试classesList.get(0)与用户输入进行比较时mage。它不会工作。

List<String> classesList = new ArrayList<String>();
classesList.add("mage");
classesList.add("warrior");
classesList.add("thief"); 

Scanner input = new Scanner(System.in);

String input = input.next();

for (int counter = 0; counter < classesList.size(); counter++) { 
    if (input == classesList.get(counter)) {
        //won't run body since can't compare
    }
}

I found some who converted the arrayList to be a String. But I am unsure how I could then easily search through my list. I'd much rather leave it as a List instead. Is the best method to this approach?

我发现有些人将 arrayList 转换为字符串。但我不确定如何轻松搜索我的列表。我宁愿把它作为一个列表。是这种方法的最佳方法吗?

String[] classesArray = classesList.toArray(new String[classesList.size()]);

采纳答案by Vallabh Patade

I think you have to see if input string exist in the ArrayList, you have to use contains()method.

我认为您必须查看 中是否存在输入字符串ArrayList,您必须使用contains()方法。

   if(classesList.contains(input)) {
       //Found in the arraylist.
   }

But with you method, you can't compare strings using ==operator. You should use equals()method for comparision

但是使用您的方法,您无法使用==运算符比较字符串。您应该使用equals()方法进行比较

List<String> classesList = new ArrayList<String>();
classesList.add("mage");
classesList.add("warrior");
classesList.add("thief"); 

Scanner input = new Scanner(System.in);

String input = input.next();
boolean isExist = false;
for (String item : classesList) { 
    if (input.equals(classesList.get(counter))) {
        //Your logic if it's there.
         isExist = true;
         break;
   }
}

 if(isExist) {
     //Found in the arraylist.
 }

Also if you want to make this comparision with ingnoring the case, you shoudl consider using equlasIgnoreCase()method instead of equals()method.

此外,如果您想将这种比较与忽略案例进行比较,您应该考虑使用equlasIgnoreCase()方法而不是equals()方法。

回答by Adam

I don't fully understand your question, however you can test if a given String exists in the list using contains()

我不完全理解您的问题,但是您可以使用contains()测试列表中是否存在给定的字符串

Some other pointers

其他一些指针

  • Never compare Strings with ==, use .equals(). See this question
  • Iteration can be done more simply using (String element : list) syntax
  • 永远不要将字符串与 == 进行比较,请使用 .equals()。看到这个问题
  • 使用 (String element : list) 语法可以更简单地完成迭代

回答by Sachin Thapa

Yes what you read is right, not to use following:

是的,你读的是对的,不要使用以下内容:

for (int counter = 0; counter < classesList.size(); counter++) { 
    if (input == classesList.get(counter)) {
        //won't run body since can't compare
    }
}

You cannot use ==for comparison instead you should use equalsmethod if you are using loop:

如果使用循环,则不能==用于比较,而应使用equals方法:

for (int counter = 0; counter < classesList.size(); counter++) { 
    if (input.equals(classesList.get(counter))) {
        //won't run body since can't compare
    }
}

Otherwise easier way is to use containsas mentioned in other answer.

否则更简单的方法是使用contains其他答案中提到的。

Also see How do I compare strings in Java?

另请参阅如何比较 Java 中的字符串?

Cheers !!

干杯!!

回答by Makoto

You can use the containsmethod on your list to see if a particular string is contained inside of it, instead of iterating across it. However, the underlying list will still be iterating over the contents of the list internally.

您可以使用contains列表中的方法查看其中是否包含特定字符串,而不是遍历它。但是,底层列表仍将在内部迭代列表的内容。

What you could do instead, to improve speed, is to use a Setinstead, which has a nice property of having containsbe run in constant time.

为了提高速度,您可以做的是使用 aSet代替,它具有contains在恒定时间内运行的良好特性。

Set<String> classesSet = new HashSet<>();
// none of your other code has to change, except for:

if(classesSet.contains(input)) {
    // logic
}