John123 John123 - 28 days ago 8
Java Question

How to output one element of an Array List?

For my assignment I have to demonstrate removing a user from an array list (done) and then going through the array list using an iterator and instead of removing a user from the list, I must output only one specific user from that list. This question is similar to my other question about removing an element but different in the way that I'm asking how to iterate through the list and output one user as opposed to removing a user form the list. I have tried using a while loop which includes an if statement and uses the .next() function but this doesn't seem to be outputting 1 user as it still outputs all users. I will share the relevant code for reproducing this part of the program:

Main class (used to call the method)

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 class (where the array list has been created)

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();
}
}
}
}


User class (details for users)

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


I've tried searching online about how to do this but I cannot seem to find a source that specifies how to output one element of the array list therefore this may help others who might be stuck on the same issue, any help is appreciated, thanks.

Answer
  • You should try to make your cade as simple as possible.

  • You DO NOT WANT to implement Iterable, if you really need to be able to iterate over your class it is Iterator that you should implement. (see this : http://stackoverflow.com/a/22357335/4088809)

  • Make the members of your class private to ensure encapsulation, and if you need to access those use the appropriate 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());
    }
  }
}
Comments