Java 通过数组列表搜索?爪哇

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20450092/
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-13 02:02:00  来源:igfitidea点击:

Searching through arraylist?? Java

javaif-statementfor-looparraylist

提问by Jess Anastasio

Hello so I have two arraylists in my problem, one filled with the top 1000 girl names and the amount of times they're used and one filled with the top 1000 boy names and the amount of times they're used. The user inputs a name, searches through both arraylists and outputs if the name is in the top boy or girl names, what their ranking is and how many times they're used. If the name is in the top girls it also needs to output The name is not a part of the top 1000 boy names and the same if it was a boy name. This is what I have

您好,我的问题中有两个数组列表,一个填充了前 1000 个女孩名字及其使用次数,另一个填充了前 1000 个男孩名字及其使用次数。用户输入一个名字,在两个数组列表中搜索并输出这个名字是否在最热门的男孩或女孩名字中,他们的排名是什么以及他们被使用的次数。如果名字在前1000名男孩名字中,它也需要输出名字不是前1000名男孩名字的一部分,如果是男孩名字也一样。这就是我所拥有的

  public void search()
  {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter the name you would like to search.");
    nameSearch = keyboard.next();

    for(int i = 0; i < girls.size(); i++)
    {
      if(nameSearch.equals(girls.get(i).getName()))
      {
        System.out.println(nameSearch + " is ranked " + (i+1) + " in popularity among girls with " + girls.get(i).getAmount() + " namings.");
      }

    }


    else
      System.out.println(nameSearch + " is not in the top 1000 girl names.");


    for(int i = 0; i < boys.size(); i++)
    {
      if(nameSearch.equals(boys.get(i).getName()))
      {
        System.out.println(nameSearch + " is ranked " + (i+1) + " in popularity among boys with " + boys.get(i).getAmount() + " namings.");
      }
    }


    else
      System.out.println(nameSearch + " is not in the top 1000 boys names.");

  }

Obviously I get an error because i can't have an else statement without an if before it.. Everything works as far as searching the array and finding the rank and whatever i just need it to output the right stuff. I am not sure at all what to do to get this to work.

显然我得到了一个错误,因为我不能有一个没有 if 的 else 语句。就搜索数组和找到排名以及我只需要它输出正确的东西而言,一切都有效。我完全不知道该怎么做才能让它发挥作用。

update- i tried the contains method and i can never get it to work. it always outputs that it can not find the name in either arrays when i know it is a name that is there. Let me know if this looks wrong because it won't work correctly

更新 - 我尝试了 contains 方法,但我永远无法让它工作。当我知道它是一个名称时,它总是输出它在任何一个数组中都找不到名称。如果这看起来不对,请告诉我,因为它无法正常工作

Thanks! --Jess

谢谢!——杰斯

   Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter the name you would like to search.");
    nameSearch = keyboard.next();

    if(girls.contains(nameSearch))
    {
      for(int i = 0; i < girls.size(); i++)
      {
        if(nameSearch.equals(girls.get(i).getName()))
        {
          System.out.println(nameSearch + " is ranked " + (i+1) + " in popularity among girls with " + girls.get(i).getAmount() + " namings.");
        }
      }
    } 
    else
      System.out.println(nameSearch + " is not in the top 1000 girl names.");



    if(boys.contains(nameSearch))
    {
      for(int i = 0; i < boys.size(); i++)
      {
        if(nameSearch.equals(boys.get(i).getName()))
        {
          System.out.println(nameSearch + " is ranked " + (i+1) + " in popularity among boys with " + boys.get(i).getAmount() + " namings.");
        }
      }
    } else
     {
      System.out.println(nameSearch + " is not in the top 1000 boys names.");
     }

回答by Jeroen Vannevel

You can check if it contains it:

您可以检查它是否包含它:

if(girls.contains(nameSearch)) {
   // Look for the data
} else {
   // print error
}

回答by Steve P.

Use a booleanto determine if you went into the condition, ie:

使用 aboolean来确定您是否进入了条件,即:

  if(nameSearch.equals(boys.get(i).getName()))
  {
       myBoolean = true;
       //...
  }

Then use:

然后使用:

 if(!myBoolean)
      System.out.println(nameSearch + " is not in the top 1000 boys names.");

回答by chuthan20

You should keep a variable to check if you have found the name or not... similar to below.

您应该保留一个变量来检查您是否找到了名称...类似于以下内容。

nameSearch = keyboard.next();

boolean didFindMatch=false;
for(int i = 0; i < girls.size(); i++)
{
  if(nameSearch.equals(girls.get(i).getName()))
  {
    System.out.println(nameSearch + " is ranked " + (i+1) + " in popularity among girls with " + girls.get(i).getAmount() + " namings.");
        didFindMatch=true;
        break;
  }

}


if (!didFindMatch)
  System.out.println(nameSearch + " is not in the top 1000 girl names.");

回答by rabz100

Seems to me you could use the arraylist.indexof method to get the index, and then if it's not -1, use the index to get the amount of times the name appears. For this to work you would need to create some sort of person object, and be able to compare it with another person an object by name.

在我看来,您可以使用 arraylist.indexof 方法来获取索引,然后如果它不是 -1,则使用索引来获取名称出现的次数。为此,您需要创建某种 person 对象,并能够通过名称将其与另一个 person 对象进行比较。

Person p = new Person(namesearch);
int index = (girls.indexOf(p);
if(index != -1) {
   // Print info
} else {
   // print error
}

回答by cmd

To quickly search your List, you can use Collections.binarySearch(...). (see here). To use binarySearchyour Collectionmust be sorted. The same Comparatorused for sort(see here) must be used by binarySearch

要快速搜索您的List,您可以使用Collections.binarySearch(...). (见这里)。要使用binarySearchCollection必须排序。相同的Comparator用于sort(见这里)必须由binarySearch

You can use binarySearch to find a specified name

您可以使用 binarySearch 来查找指定的 name

回答by deef0000dragon1

not the cleanest but should work pretty well.

不是最干净的,但应该工作得很好。

bolean inboys = false;
for(int i = 0; i < boys.size(); i++)
{
  if(nameSearch.equals(boys.get(i).getName()))
  {
    System.out.println(nameSearch + " is ranked " + (i+1) + " in popularity among boys with " +            boys.get(i).getAmount() + " namings.");
inboys = true;

  }
}

if(inboys == false)
{
  System.out.println(nameSearch + " is not in the top 1000 boys names.");
}

回答by Syd

I think your conditions should something like these. . .

我认为你的条件应该是这样的。. .

int rank = 0;
int amount = 0;

for(int i = 0; i < girls.size(); i++)
{
  if(nameSearch.equals(girls.get(i).getName()))
  {
    rank = i + 1;
    amount = girls.get(i).getAmount();
    break;
  }

}
if (rank > 0 && amount > 0) {
    System.out.println(nameSearch + " is ranked " + rank + " in popularity among girls with " + amount + " namings.");
} else {
    for(int i = 0; i < boys.size(); i++) {
       if(nameSearch.equals(boys.get(i).getName())) {
           rank = i + 1;
           amount = boys.get(i).getAmount();
           break;
       }
    }
    if (rank > 0 && amount > 0) {
        System.out.println(nameSearch + " is ranked " + rank + " in popularity among boys with " + amount + " namings.");
    } else {
        System.out.println(nameSearch + " is not in the top 1000 girl names.");
        System.out.println(nameSearch + " is not in the top 1000 boys names.");
    }
}

回答by chaitanya89

contains() method is the best you could do.

contains() 方法是您能做的最好的方法。

As Jeroen Vannevel mentioned,

正如 Jeroen Vannevel 所说,

First search the name in girls arraylist and then boys arraylist like:

首先在女孩数组列表中搜索名称,然后在男孩数组列表中搜索名称,例如:

if(girls.contains(nameSearch))
{
    //print your result
} else if(boys.contains(nameSearch))
{
   //print your result
} else
{
   //print not found.
}

You don't have to loop through the whole arraylist.

您不必遍历整个数组列表。

Java has two types of contains method

Java有两种包含方法

1 - boolean java.lang.String.contains(CharSequence s)

1 - 布尔 java.lang.String.contains(CharSequence s)

2 - public boolean contains(Object o)

2 - 公共布尔包含(对象 o)

In your case it's a String object, choose 2nd type of contains() and it returns true if the String is found. You could get what you wanted.

在您的情况下,它是一个 String 对象,选择第二种类型的 contains() 并且如果找到 String 则返回 true 。你可以得到你想要的。