gpt4 book ai didi

java - 检查对象 ArrayList 中的字符串

转载 作者:行者123 更新时间:2023-12-01 18:10:06 24 4
gpt4 key购买 nike

我在查找 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;
}
}

最佳答案

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

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

关于java - 检查对象 ArrayList 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33686395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com