我如何从arraylist中获取特定的字符串变量

时间:2017-11-28 11:49:30

标签: java eclipse arraylist

public class Suspect extends Registry {
  private String name; 
  private String code_name; 
  private String city; 

  public Suspect(String name, String code_name, String city) {
    this.name = name;
    this.code_name = code_name;
    this.city = city;
  }

  ArrayList<String> numbers = new ArrayList<String>();

  public void addNumber(String number) {
    numbers.add(number);
  }

  ArrayList<Suspect> partners = new ArrayList<Suspect>(); 

  public void IsPartner(Suspect partner) {
    //if
    partners.add(partner);
  }

  public boolean isConnectedTo(Suspect aSuspect) {
    if (partners.contains(aSuspect)) 
      return true;
    else 
      return false; 
  }

  ArrayList<Suspect> CommonSuspects = new ArrayList<Suspect>();

  public ArrayList<Suspect> getCommonPartners(Suspect aSuspect) {
    for (Suspect partner: partners) {
      Suspect ProbablyCommonSuspect = partner; 
      if (partner.isConnectedTo(ProbablyCommonSuspect) && (aSuspect.isConnectedTo(ProbablyCommonSuspect)))  
        CommonSuspects.add(partner);    
    }
    return CommonSuspects;
  }
  public String getName() {

}

我在java中犯罪网有问题。我想创建一个像getName这样的方法,我可以从中获取类型为Suspect的伙伴的名称,并将其返回给main方法。我该如何设法做到这一点?

如果您在课堂上看到任何其他问题,请告诉我。 (业余人员)

1 个答案:

答案 0 :(得分:-1)

public static String getName(ArrayList<Suspect> arr,String name) {
    for(int i=0;i<arr.size();i++){
                  if(arr.get(i).getName().equals(name)){

         return arr.get(i).getName();

          }
      }
    return null;
  }