检查字符串的ArrayList对象

时间:2015-11-13 05:18:21

标签: java arraylist

我无法通过FacebookPerson对象的ArrayList查找字符串(名称)。这是我第一个使用ArrayLists的项目,因此我充其量只是对我正在做的事情的一个基本概念。我不理解的是为什么fbUsers.contains(name)似乎没有做任何事情,以及我如何正确搜索字符串名称。

--- testFacebook_ArrayList.java

package Facebook;

import java.util.*;

public class testFacebook_ArrayList {
    public static Scanner Input = new Scanner(System.in);

    public static void main(String[] args){
        String name, mood;
        boolean exit = false;
        ArrayList<FacebookPerson> fbUsers = new ArrayList<FacebookPerson>();

        while(!exit){
        System.out.print("Enter the name of a facebook user to be created (enter #### to end creation & move to user selection): ");
        name = Input.next();
        Input.nextLine();

            if(name.equals("####")){
            exit = true;
            }
            else if(fbUsers.contains(new FacebookPerson(name))){
                System.out.println("Error, name already exists. Try again.");
                continue;
            }
            else{
                fbUsers.add(new FacebookPerson(name));
            }
        }
        exit = false;
        while (!exit){
            System.out.print("Enter a user's name to modify their mood (#### to terminate the program): ");
            name = Input.nextLine();

            if (name.equals("####")){
                System.out.println("Program terminated.");
                System.exit(1);
            }
            else if (fbUsers.contains(new FacebookPerson(name))){
                System.out.print("Enter a mood for " + name + ": ");
                mood = Input.nextLine();
                for (int i = 0; i < fbUsers.size(); i++){
                    if(fbUsers.get(i).equals(new FacebookPerson(name))){
                        fbUsers.get(i).setMood(mood);
                    }
                }
            }
            else{
                System.out.println("Unrecognized name. Try again.");
                continue;
            }
        }
    }
}

--- FacebookPerson.java

  // This is the FacebookPerson_Graphics class
  // Written by Dr. Xiaolin Hu
  // 03/05/2015
  package Facebook;

  public class FacebookPerson{

  private String myName;
  protected String myMood;
  protected Facebook myfacebook;

  public FacebookPerson(String name){
      myName = name;
      myfacebook = new Facebook(myName);
      //System.out.println("FacebookPerson's constructor");
  }

  public FacebookPerson(){

  }

  public String getName(){
      return myName;
  }

  public void setMood(String newMood){
     myMood = newMood;
     myfacebook.setContent(myMood);
  }

  public String getMood(){
      return myMood;
  }

}

--- Facebook.java

// This is the Facebook class
// Wrriten by Dr. Xiaolin Hu
// 03/05/2015
package Facebook;

import java.awt.*;

public class Facebook{

   private String name;
   private String content;
   DrawingPanel panel;
   private Graphics g;

   public Facebook(String nm){
       content = "undefined";
       name = nm;

       // Create the drawing panel
       panel = new DrawingPanel(200, 150);
       g = panel.getGraphics();

       // display name
       g.drawString(name+"'s mood is undefined.", 20, 75);
   }

   public void setContent(String newContent){
    content = newContent;
        if(content.equals("happy")){
            g.setColor(Color.red);
            g.fillRect(0, 0, 200, 150);
            g.setColor(Color.black);
            // display mood
            g.drawString(name+"'s mood is:"+ "happy", 20, 75);
        }
        else{
            g.setColor(Color.white);
            g.fillRect(0, 0, 200, 150);
            g.setColor(Color.black);
            g.drawString(name+"'s mood is:"+ content, 20, 75);
        }
   }

   public String getContent(){
    return content;
   }
}

3 个答案:

答案 0 :(得分:3)

您可以为每个用户执行for循环并调用getName();

for (FacebookPerson fbp : fbUsers ){
     if (fbp.getName().equals(name)){
          System.out.println("Error, name already exists. Try again.");
          continue;
     }
}

答案 1 :(得分:1)

您可以使用二维数组列表来存储信息并将其链接到名称。那样可以解决你的问题

答案 2 :(得分:0)

Calderious,

您需要覆盖并实施FacebookPerson的equals(...)方法才能使包含起作用。如果你查看List的contains(...) javadoc,它会特别提到:

  

...当且仅当此集合包含至少一个时才返回true   元素e使(o==null ? e==null : o.equals(e))

在课堂上添加以下内容

public class FacebookPerson {
  ...

  @Override
  public boolean equals(Object obj) {
    // your equals logic goes here
  }

  ...
}

甚至更好,IDE现在可以根据您声明的属性创建它。在Eclipse中,您只需右键单击代码&gt; Source&gt; Generate hashCode() and equals()

我希望有所帮助。