对于我的任务,我必须演示从数组列表中删除用户(完成),然后使用迭代器浏览数组列表,而不是从列表中删除用户,我必须只输出该列表中的一个特定用户。这个问题类似于我关于删除一个元素的另一个问题,但与我询问如何遍历列表并输出一个用户而不是从列表中删除用户的方式不同。我尝试使用包含if语句的while循环并使用.next()函数,但这似乎并没有输出1个用户,因为它仍然输出所有用户。我将分享用于复制该部分程序的相关代码:
主类(用于调用方法)
package main;
public class Main{
public static void main(String[] args) {
System.out.println("***********Iteration of administrators***********");
UserGroup2 userGroupObject2 = new UserGroup2();
for (User secondusergroup : userGroupObject2.getUserGroup())
{
userGroupObject2.getUserIterator();
System.out.println(secondusergroup.toString());
}
}
}
}
UserGroup2类(已创建数组列表)
package main;
import java.util.ArrayList;
import java.util.Iterator;
public class UserGroup2 implements Iterable<UserGroup2> {
ArrayList<User> administrators = new ArrayList<>();
public UserGroup2()
{
addUser(new User("lnb1g16", "Student", "Lee"));
addUser(new User("hpf1g17", "Staff", "Harry"));
addUser(new User("jks1g25", "Student", "Jordon"));
addUser(new User("ohw1237", "Admin", "Oliver"));
addUser(new User("ahs1213", "Student", "Jordon"));
addUser(new User("bdh1285", "Admin", "Bob"));
addUser(new User("tqn1294", "Student", "Taylor"));
}
public void addUser(User inUser)
{
//userGroup.add(new User("LeeB123", "Staff", "Lee"));
administrators.add(inUser);
}
public ArrayList<User> getUserGroup() {
return administrators;
}
public void getUserIterator() {
Iterator<User> it2 = administrators.iterator();
while (it2.hasNext()) {
User xxx = it2.next();
if (xxx.getUsername().equals("jks1g25")) {
it2.next();
}
}
}
}
用户类(用户详细信息)
package main;
class User {
String username;
String userType;
String name;
User(String username, String userType, String name) {
this.username = username;
this.userType = userType;
this.name = name;
}
public String getUsername() {
return username;
}
public String getUserType() {
return userType;
}
public String getName() {
return name;
}
public String setUserType(String admin) {
return userType = "admin";
}
@Override
public String toString() {
return username + " " + userType;
}
}
我已尝试在线搜索如何执行此操作,但我似乎无法找到指定如何输出数组列表中的一个元素的源,因此这可能有助于其他可能遇到同一问题的人,任何非常感谢,谢谢。
答案 0 :(得分:0)
我会尝试逐步解释。
// This does nothing. It doesn't modify the array, and it doesn't print anything
userGroupObject2.getUserIterator();
// This will just output the user name and type
System.out.println(secondusergroup.toString());
基本上,这已经完成了你的任务:
for (User user: userGroupObject2.getUserGroup()) {
if ("jks1g25".equals(secondusergroup.getUsername()) {
System.out.println(secondusergroup);
break;
}
}
如果必须明确使用迭代器:
Iterator<User> it = userGroupObject2.getUserGroup().iterator();
while (it.hasNext()) {
User u = it.next();
if ("jks1g25".equals(secondusergroup.getUsername()) {
System.out.println(secondusergroup);
break;
}
}
答案 1 :(得分:0)
你应该尽量使你的cade变得简单。
你不想实现Iterable,如果你真的需要能够迭代你的类,那么你应该实现Iterator。 (见:https://stackoverflow.com/a/22357335/4088809)
将您的类成员设为私有以确保封装,如果您需要访问它们,请使用相应的getter。
<强> User.java 强>
package main;
public class User {
//Set the members of this class to private
//This is to ensure encapsulation
private String username;
private String userType;
private String name;
User(String username, String userType, String name) {
this.username = username;
this.userType = userType;
this.name = name;
}
public String getUsername() {
return username;
}
public String getUserType() {
return userType;
}
public String getName() {
return name;
}
//Set the usertype using the admin parameter
//instead of the string admin
//otherwise all the users will be admin
public String setUserType(String admin) {
return userType = admin;
}
@Override
public String toString() {
return username + " " + userType;
}
}
<强> UserGroup2.java 强>
package main;
import java.util.ArrayList;
//no need to implement Iterator, since you only add element
//to an arraylist and nothing else.
public class UserGroup2 {
private ArrayList<User> administrators = new ArrayList<>();
public UserGroup2() {
addUser(new User("lnb1g16", "Student", "Lee"));
addUser(new User("hpf1g17", "Staff", "Harry"));
addUser(new User("jks1g25", "Student", "Jordon"));
addUser(new User("ohw1237", "Admin", "Oliver"));
addUser(new User("ahs1213", "Student", "Jordon"));
addUser(new User("bdh1285", "Admin", "Bob"));
addUser(new User("tqn1294", "Student", "Taylor"));
}
public void addUser(User inUser) {
this.administrators.add(inUser);
}
public ArrayList<User> getAdministrators() {
return administrators;
}
}
<强> Main.java 强>
package main;
public class Main{
public static void main(String[] args) {
System.out.println("***********Iteration of administrators***********");
//Create the user group
UserGroup2 userGroupObject2 = new UserGroup2();
//Get and iterate over the arraylist inside usergroup2
for (User currentUser : userGroupObject2.getAdministrators()) {
//Compare the current user to the username we are searching for
if (currentUser.getUsername().equals("jks1g25"))
System.out.println(currentUser.toString());
}
}
}