Java ArrayList不存储值
我正在创建一个邮件系统,但有一个小错误我想不出来。我张贴了所有的课程,以防任何正在阅读的人需要它来理解任何东西,但是我显然不希望人们通读所有的课程 出现的问题是,当我运行main时,我能够创建一个用户名,但是在创建它之后,用户名没有被存储 我认为错误要么在main类中,要么在UserList类中,但我就是找不到它。我把评论放在我认为错误的地方 抱歉,如果我的帖子太长和/或格式不正确。我会拿出任何不需要的代码,只要告诉我需要什么Java ArrayList不存储值,java,arraylist,Java,Arraylist,我正在创建一个邮件系统,但有一个小错误我想不出来。我张贴了所有的课程,以防任何正在阅读的人需要它来理解任何东西,但是我显然不希望人们通读所有的课程 出现的问题是,当我运行main时,我能够创建一个用户名,但是在创建它之后,用户名没有被存储 我认为错误要么在main类中,要么在UserList类中,但我就是找不到它。我把评论放在我认为错误的地方 抱歉,如果我的帖子太长和/或格式不正确。我会拿出任何不需要的代码,只要告诉我需要什么 /** * Created by Broomhead0 on 4/
/**
* Created by Broomhead0 on 4/11/14.
*/
import java.util.ArrayList;
public class Userlist
{
//private User[] users; // this is an array that will store references to all users
ArrayList<User> users; //this is an arraylist that will store references to all users
private int numberUsr; // this is the number of users that is currently stored in the user. Always smaller or equal to maxUser
public Userlist()
{
users = new ArrayList<User>();
numberUsr = 0; // at start no user stored yet
}
// find a user in the list based on the username
public User findUser(String username)
{
// iterate through the array; only iterate according to how many users are currently in the array
for (int i = 0; i < numberUsr; i++)
{
// access the particular user through users.(i), then get the name,
// and call the equals method on that name to compare it with the input username
if (users.get(i).userName.equals(username)){
return users.get(i);
}
}
// no user found
return null;
}
//ERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERROR
// add a user to the list; only do so if the user does not yet exist
public void addUser(User u)
{
if (findUser(u.userName) != null) //if there is a match,
System.out.println("User already exists");
else //if there is not match
{
users.add(u); //add the username
}
//ERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERRORERROR
}
}
/**
*由Broomhead0于2014年4月11日创建。
*/
导入java.util.ArrayList;
公共类用户列表
{
//私有用户[]用户;//这是一个将存储对所有用户的引用的数组
ArrayList users;//这是一个ArrayList,它将存储对所有用户的引用
private int NUMBERRUSR;//这是当前存储在用户中的用户数。始终小于或等于maxUser
公共用户列表()
{
users=newarraylist();
numberRusr=0;//开始时尚未存储任何用户
}
//根据用户名在列表中查找用户
公共用户findUser(字符串用户名)
{
//迭代数组;仅根据数组中当前有多少用户进行迭代
对于(int i=0;i
添加新用户后,NumberUser保持为0。添加新用户时增加该值,或者在用户上循环时最好使用users.size()
public User findUser(String username)
{
// iterate through the array; only iterate according to how many users are currently in the array
for (int i = 0; i < users.size(); i++)
{
// access the particular user through users.(i), then get the name,
// and call the equals method on that name to compare it with the input username
if (users.get(i).userName.equals(username)){
return users.get(i);
}
}
// no user found
return null;
}
公共用户findUser(字符串用户名)
{
//迭代数组;仅根据数组中当前有多少用户进行迭代
对于(int i=0;i
您在此处发布了大量代码。请尝试将其简化为一个小得多的程序,该程序仍然可以演示您的问题。创建一个最小的、完整的、可验证的示例:尝试编写一个小(如10行)程序来演示问题。这将帮助您缩小问题的范围,并帮助我们轻松地解决问题。没有人会通读代码页来帮助您了解一个小细节。添加到Arraylist后,从Arraylist中搜索新用户,并检查它是否存在。它将帮助您调试您的numberRUSR
字段是多余的ArrayList
维护其元素本身的计数(size():int
集合界面中的方法)。感谢@quaertym和Krzysztof的快速回答!