简体   繁体   English

如何使用 ArrayList.contain() 检查 java 中的 object

[英]How to use the ArrayList.contain() to check the object in java

I am practicing the ArrayList with contains method.我正在使用包含方法练习 ArrayList。 For checking the objects in the ArrayList, every time I need to loop through the entire ArrayList using for each loop.为了检查 ArrayList 中的对象,每次我需要使用每个循环遍历整个 ArrayList 时。 Therefore, I am thinking if there is a way to do such a check using contains method.因此,我在想是否有办法使用contains方法进行此类检查。

I've referred to this post: How does a ArrayList's contains() method evaluate objects?我参考过这篇文章: ArrayList 的 contains() 方法如何评估对象?

But I am new to java and don't really the meaning of the method.但我是 java 的新手,并不真正了解该方法的含义。 Does it mean to override the equals() method to check the object?是否意味着重写equals()方法来检查object? Any help is highly appreciated.非常感谢任何帮助。

Here is my method trying to check if the user exists in the playerList .这是我尝试检查用户是否存在于playerList中的方法。

public void showCertainPlayerInformation(ArrayList<NimPlayer> playerList, String inputName)
    {
        System.out.println("The request to show the name: "+ inputName);
        
        //trying to use the contains method, but it seems impossible
        //if (playerList.contains(NimPlayer.getUserName().equals(inputName)))

        for (NimPlayer player : playerList) 
        {
            String userCheck = player.getUserName();
            String familyName = player.getFamilyName();
            String givenName= player.getGivenName();
            int gamesWon = player.getGamesWon();
            int gamesPlayed = player.getGamesPlayed();
            
            if (userCheck.equals(inputName)) 
            {
                System.out.println(inputName + "," + givenName + "," 
                + familyName + "," + gamesPlayed + " games," + gamesWon + " wins");
                return;
            } 
                 
        }

        System.out.println("The player does not exist");
    }

Yes.是的。 You need to define your NimPlayer class like this (if you want to compare two NimPlayer objects only by their userName)您需要像这样定义您的 NimPlayer class (如果您只想通过用户名比较两个 NimPlayer 对象)

package com.example.schooltimetable;

import java.util.Objects;

public class NimPlayer {
  private String userName;
  private String familyName;
  private String givenName;
  private int gamesWon;
  private int gamesPlayed;

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getFamilyName() {
    return familyName;
  }

  public void setFamilyName(String familyName) {
    this.familyName = familyName;
  }

  public String getGivenName() {
    return givenName;
  }

  public void setGivenName(String givenName) {
    this.givenName = givenName;
  }

  public int getGamesWon() {
    return gamesWon;
  }

  public void setGamesWon(int gamesWon) {
    this.gamesWon = gamesWon;
  }

  public int getGamesPlayed() {
    return gamesPlayed;
  }

  public void setGamesPlayed(int gamesPlayed) {
    this.gamesPlayed = gamesPlayed;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o)
      return true;
    if (o == null || getClass() != o.getClass())
      return false;
    NimPlayer nimPlayer = (NimPlayer) o;
    return Objects.equals(userName, nimPlayer.userName);
  }

  @Override
  public int hashCode() {
    return Objects.hash(userName);
  }
}

Now to check if the arraylist contains:现在检查 arraylist 是否包含:

public void showCertainPlayerInformation(ArrayList<NimPlayer> playerList, String inputName)
    {
      NimPlayer n = new NimPlayer();
      n.setUserName(inputName);
      if(playerList.contains(n)){
          ....
          return;
      }
      System.out.println("The player does not exist");
     }

You can go to method declaration (CTRL + click on method name) and check its implementation.您可以 go 到方法声明(CTRL + 单击方法名称)并检查其实现。 contains() uses equals() to check whether passed object equals any of the elements or not. contains()使用equals()检查传递的 object 是否等于任何元素。 And equals() declaration can be found in Object class:equals()声明可以在Object class 中找到:

ArrayList Class: ArrayList Class:

public boolean contains(Object var1) {
    return this.indexOf(var1) >= 0;
}

public int indexOf(Object var1) {
    int var2;
    if (var1 == null) {
        for(var2 = 0; var2 < this.size; ++var2) {
            if (this.elementData[var2] == null) {
                return var2;
            }
        }
    } else {
        for(var2 = 0; var2 < this.size; ++var2) {
            if (var1.equals(this.elementData[var2])) {
                return var2;
            }
        }
    }

    return -1;
}

Object Class: Object Class:

public boolean equals(Object var1) {
    return this == var1;
}

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

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