2016-11-04 31 views
0
package main; 
import java.util.ArrayList; 

public class UserGroup { 

    ArrayList<User> userGroup = new ArrayList<>(); 
    User userOne; 
    public UserGroup() { 
    addUser(new User("lnb1g16", "Student", "Lee")); 
    addUser(new User("hpf1g17", "Staff", "Harry")); 
    addUser(new User("jks1g25", "Student", "Jordon")); 
    } 

    public void addUser(User inUser) { 
    //userGroup.add(new User("LeeB123", "Staff", "Lee")); 
    userGroup.add(inUser); 
    } 

    public ArrayList<User> getUserGroup() { 
    return userGroup; 
    } 

    public void removeFirstUser() { 
    userGroup.remove(0) 
    } 
} 

我試過創建一個方法,刪除第一個用戶,似乎是編譯但實際上並沒有刪除用戶程序編譯時。任何關於如何解決這個問題的想法都很好,謝謝。如何從數組列表中刪除用戶?

可以調用程序宣告用戶的詳細信息

package main; 

public class Main{ 
    public static void main(String[] args) { 
    for (int counter=2; counter<=40; counter+=2) { 
     System.out.println(counter); 
    } 
    System.out.println("For loop complete."); 

    int counter = 1; 
    int increment = 2; 
    int loopexeccounter = 0; 
    while (counter <= 500) { 
     loopexeccounter = loopexeccounter + 1; 
     System.out.println(counter); 
     counter = counter + increment++; 
    } 
    System.out.print("This loop iteratted "+loopexeccounter+" times.\n"); 

    { 
     //public User callUserGroup; 
     UserGroup userGroupObject = new UserGroup(); 

     for (User curUser : userGroupObject.getUserGroup()) { 
     System.out.println(curUser.toString()); 
     } 
    } 
    } 
} 

用戶的方法主要方法

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

你如何知道它不會被移除? – developer

+0

當我運行程序時,仍然可以看到第一個用戶 – John123

+0

的詳細信息,該程序在哪裏? – developer

回答

0

它的工作好:

` public static void main(String[] args) { 
    UserGroup MyUser = new UserGroup(); 
    for (User curUser : MyUser.getUserGroup()) { 
     System.out.println(curUser.toString()); 
     } 
    MyUser.removeFirstUser(); 
    System.out.println("your user removed!"); 
    for (User curUser : MyUser.getUserGroup()) { 
     System.out.println(curUser.toString()); 
     } 
    }` 

結果:

`[email protected] 1 
[email protected] 2 
[email protected] 3 
your user removed! 
[email protected] 1 
[email protected] 2`