【问题标题】:Searching for a string in an ArrayList object [duplicate]在 ArrayList 对象中搜索字符串 [重复]
【发布时间】:2020-03-21 04:49:29
【问题描述】:

我有一个 Player 类型的 ArrayList,我正在尝试搜索特定字符串,但遇到了很多麻烦。

目前我使用 boolean 来查看播放器是否包含字符串。然后我使用 for loop 来搜索列表。

但它一直在输出文件的最后一行;我很确定我没有正确搜索。

String playerName = sc.next();
    boolean isFound = players.contains(playerName);
    int index = 0;
    for (int i = 0; i < players.size(); i++) {
        if (isFound) {
            break;
        }
        index = i;

    }
    System.out.println(players.get(index));
    System.out.println(isFound);

我的 Player 类定义如下 (不确定这是否有帮助)

public class Player extends Person {
    public String pos;
    public int g;
    public int ab;
    public int r;  // various statistics for the player

    public Player(String name, String team, String pos) {
        this.name = name;
        this.team = team;
        this.pos = pos;
    }

我还有一个包含列表的联赛课程

public class League extends loadData {
public static ArrayList<Player> players = new ArrayList<Player>();
public static ArrayList<Pitcher> pitchers = new ArrayList<Pitcher>();

}

如果有人可以帮助我,将不胜感激!

【问题讨论】:

  • int index = players.indexOf(playerName); boolean isFound = index &gt;= 0;.
  • 在迭代时在循环内执行boolean isFound = players.contains(playerName);
  • 你需要比较的“type Player”的属性是什么?
  • 您能否提供测试用例(应该已经找到):请包含 playerName 和您观察到的匹配项!可能是字符编码的问题。
  • 您能否更新您的帖子以声明 class Player 以便我们现在列出列表元素的类型。这会影响搜索和结果!

标签: java string search


【解决方案1】:

你的代码发生了什么

isFound仅在循环之前设置。检查string is contained within the list是否完全没问题。

如果列表不包含字符串,则循环将永远不会中断并遍历所有元素。所以在这种情况下,您的索引计数器i指向最后一个元素,然后在循环后打印。

明确列表元素的类型 VS 搜索的类型

如果您的列表存储Player 的对象,您不能简单地将它们与搜索的对象String 进行比较。也许您可以 - 在列表的每个元素上 - 比较 player.toString().contains(playerName) 或者您可以检查 player.getName().equalsIgnoreCase(playerName)

您列出的元素的类型及其声明事项,需要(我们)知道才能解决这个问题。

解决搜索和打印问题

假设列表包含字符串类型的元素,即ArrayList&lt;String&gt; players

要检查是否包含元素(此处为字符串 playerName),您可以使用 (Array)List 的 indexOfcontains 方法。 注意:该字符串可以是列表中包含的 0、1 或多次。上面的方法只找到第一个或没有找到。

对于打印,如果找到,您可以直接打印搜索词 playerName。或者您可以使用方法调用index 返回的第一个找到的索引来获取匹配的元素并打印它。 注意:两个选项中打印的文本应该相同,尽管 instance-ids/hashCodes 可能会有所不同。

【讨论】:

  • 即便如此,仅当它是一个字符串列表,并且如果目标是确定其中一个字符串是否与玩家姓名完全相等,而不是其子字符串。
  • 嗯,所有这些都是有道理的,但是字符串包含在数组中,所以我认为循环应该中断。我还用完整的字符串尝试了它,它仍然输出了最后一行。所以我确定问题出在我的循环中
  • 在问题中提到“玩家类型的arrayList”,从语法上讲,这意味着 ArrayList 即不是 List
  • @wissamhammoud 如果你搜索包含的部分字符串,这需要循环和element.contains(partialPlayerName)
  • @Jayr 完全了解这个细节。方法从接口 List 继承或覆盖到类 ArrayList。因此它们的工作方式相同。
【解决方案2】:

如果您只需要列表中某个元素的索引,请使用List#indexOf()。无需先检查列表是否包含该元素,因为如果不包含,则该方法返回-1

例子:

List<String> players = new ArrayList<String>
players.addAll(Arrays.asList("Jim, "Bob, "Mary");

int indexOfBob = players.indexOf("Bob");

如果您计划遍历列表并对所有元素执行操作直到到达特定元素,则不需要使用两个索引整数。示例:

int i;
for(i = 0; i < players.size(); i++) {
    // Perform an operation on the current player.

    if(players.get(i).equals("Bob")) break;
}

【讨论】:

    猜你喜欢
    • 2015-05-26
    • 2016-01-07
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2013-10-27
    • 2012-01-01
    相关资源
    最近更新 更多