java - How to output one element of an Array List? -
for assignment have demonstrate removing user array list (done) , going through array list using iterator , instead of removing user list, must output 1 specific user list. question similar other question removing element different in way i'm asking how iterate through list , output 1 user opposed removing user form list. have tried using while loop includes if statement , uses .next() function doesn't seem outputting 1 user still outputs users. share relevant code reproducing part of program:
main class (used call method)
package main; public class main{ public static void main(string[] args) { system.out.println("***********iteration of administrators***********"); usergroup2 usergroupobject2 = new usergroup2(); (user secondusergroup : usergroupobject2.getusergroup()) { usergroupobject2.getuseriterator(); system.out.println(secondusergroup.tostring()); } } } }
usergroup2 class (where 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 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 how cannot seem find source specifies how output 1 element of array list therefore may others might stuck on same issue, appreciated, thanks.
you should try make cade simple possible.
you not want implement iterable, if need able iterate on class iterator should implement. (see : https://stackoverflow.com/a/22357335/4088809)
make members of class private ensure encapsulation, , if need access use appropriate getter.
user.java
package main; public class user { //set members of class private //this 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 usertype using admin parameter //instead of string admin //otherwise users 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 implement iterator, since add element //to arraylist , 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 user group usergroup2 usergroupobject2 = new usergroup2(); //get , iterate on arraylist inside usergroup2 (user currentuser : usergroupobject2.getadministrators()) { //compare current user username searching if (currentuser.getusername().equals("jks1g25")) system.out.println(currentuser.tostring()); } } }
Comments
Post a Comment