2016-11-05 50 views
1

對於我的任務,我必須演示如何從數組列表中刪除用戶(完成),然後使用迭代器遍歷數組列表,而不是從列表中刪除用戶,我只能從該列表中輸出一個特定用戶。這個問題類似於我關於刪除元素的另一個問題,但與我要求如何遍歷列表並輸出一個用戶的方式不同,而不是從列表中刪除用戶。我曾嘗試使用包含if語句並使用.next()函數的while循環,但這似乎不會輸出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

而不是'if2.next()'裏面的'if',你不能'System.out.println(xxx);'? – 4castle

+0

看看ArrayList文檔:https://docs.oracle。com/javase/7/docs/api/java/util/ArrayList.html#get(int) – giZm0

+0

我擁有它,因此它獲得了列表的第6次迭代,但它似乎在每次經過時都會打印此迭代陣列列表,並且它會通過每個用戶的陣列列表,因此它將爲程序中的每個用戶打印此迭代。任何人都知道如何解決這個問題,以便它只打印一次? – John123

回答

0
  • 你應該儘量讓你的凱德一樣簡單儘可能。

  • 你不想實現Iterable,如果你真的需要能夠迭代你的類,那麼你應該實現Iterator。 (請參閱:https://stackoverflow.com/a/22357335/4088809

  • 使您的類的成員爲private以確保封裝,並且如果您需要訪問這些類,請使用適當的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()); 
    } 
    } 
} 
+0

這個工作,我會通過代碼並分析如何正確地做到這一點,非常感謝您的幫助。 – John123

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; 
    }  
} 
+0

如果我把它放在主類或UserGroup2類中,這些都不起作用,不太清楚爲什麼 – John123

相關問題