遍历集合:
/**
* 遍历集合
*/
@Override
public void doPrintAll() {
for (User u : userList) { //高级for循环
System.out.println(u);
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f3907a4cb58add243f9cfe58571d092b.png)
修改数据操作
@Override
public boolean upUser(int uid) {
try {
Iterator it = userList.iterator(); //取出容器中的所有元素
while (it.hasNext()) { //判空操作,相当于数组的判空
User user = (User) it.next(); //取出元素,强制为User
if (user.getUid() == uid) { //判断取出的id是否存在
User user1 = new UserUtil().getUser("更新后:"); //将修改的user传入到user1
//计算机识别不了取出来的object,
//将修改的user1的uid传入到user
user.setUsername(user1.getUsername()); //将修改的user1的username传入到user
user.setPassword(user1.getPassword()); //将修改的user1的password传入到user
}
}
return true;
} catch (Exception e) {
return false;
}
}