gpt4 book ai didi

java - 编写一个从 arraylist 返回迭代器的方法

转载 作者:行者123 更新时间:2023-12-02 06:33:17 24 4
gpt4 key购买 nike

我正在使用 blueJ 来编写此内容。我想做的是 ddd 一个名为 getUserIterator() 的方法至UserGroupreturns an Iterator来自ArrayList并使用 Iterator我的方法返回以循环现有用户并添加 userType 的任何用户是 "admin"到管理员组。我不知道该怎么做!我的代码如下:

用户类别:

package user; 
public class User{
public enum UserType{
ADMIN, EDITOR, USER;
}

private String id;
private UserType userPermissions;
private String actualName;

public User(String username, UserType userType, String name){
id = username;
userPermissions = userType;
actualName= name;
}

public String getUsername(){
return id;
}

public UserType getUserType(){
return userPermissions;
}

public String getName(){
return actualName;
}

public void setUserType(UserType input){
userPermissions = input;
}
}

userGroup 类是:

package user;
import java.util.*;
import user.User.UserType;

public class UserGroup{

private ArrayList<User> people;

public UserGroup(){
people = new ArrayList<User>();
}

public void addSampleData(){
people.add(new User("jar1g13", UserType.ADMIN,"Jonny"));
people.add(new User("ao9", UserType.EDITOR,"Aniruddh"));
people.add(new User("pe6", UserType.USER,"Peter"));
people.add(new User("mat73", UserType.USER,"Matthew"));
people.add(new User("ora69", UserType.EDITOR,"Oranthi"));
people.add(new User("ben12", UserType.USER,"Benedict"));
people.add(new User("cam30", UserType.ADMIN,"Cambyse"));
people.add(new User("are20", UserType.USER,"Alex"));
people.add(new User("lim19", UserType.USER,"Liam"));
people.add(new User("ada13", UserType.EDITOR,"Adam"));
}

public User getUser(int idx){
return people.get(idx);
}

public void printUsernames(){
for (User user: people){
System.out.printf("%s %s\n", user.getUsername(), user.getUserType());
}
}

public void removeFirstUser(){
people.remove(0);
}

public void removeLastUser(){
people.remove(people.size()-1);
}

public void removeUser(String username){
Iterator<User> itr = people.iterator();
while (itr.hasNext()){
User element = (User) itr.next();
if (element.getUsername().equals(username)){
itr.remove();
}
}
}

}

主类:

package user;

public class Main{

public static void main(String[] args){
UserGroup grp = new UserGroup();
grp.addSampleData();
grp.printUsernames();
}

public static void admins(){
UserGroup administrators - new UserGroup();
}
}

最佳答案

我会把它写成

public class UserGroup implements Iterable<User> {
private final List<User> people = new ArrayList<User>();

public Iterator<User> iterator() {
return people.iterator();
}
// OR to prevent the list being modified.
public Iterator<User> iterator() {
return Collections.unmodifableList(people).iterator();
}
// OR for extra paranoia
public Iterator<User> iterator() {
return Collections.unmodifableList(new ArrayList<Person>(people)).iterator();
}
// OR for even more paranoia
public Iterator<User> iterator() {
List<People> ret = new ArrayList<People>();
for(People p : people)
ret.add(new Person(p)); // take a deep copy.
return Collections.unmodifableList(ret).iterator();
}
}

// later
UserGroup userGroup = ...
for(User u : userGroup) {
// do something with u
}

这假设您不需要担心调用者修改列表(这是永远不应该做的,但有些人担心这样的事情)

关于java - 编写一个从 arraylist 返回迭代器的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19891441/

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