Note that there are some explanatory texts on larger screens.

plurals
  1. POright way to use collections in Java?
    primarykey
    data
    text
    <p>I have the following code. However I am doubting about if it is the right way to implement it or not.</p> <p>What I mean is: in the collection framework there are many data structures to use and creating the class (<code>MemberList</code>) to manage the aggregations of many members can be implemented using <code>ArrayList</code>, <code>LinkedList</code>, priority queue ...</p> <p>I would like to use a data structure that fits with my needs, and that has the least Big O possible when it comes to searching, sorting, deleting, adding, modifying and deleting.</p> <pre><code>public class MemberList{ /** *a list of accounts existing in the database */ private static List&lt;Member&gt; members = new ArrayList&lt;Member&gt;(); /** * add a member to our member list * @param m the member to be added */ public static void Add(Member m) { members.add(m); /** * delete a member from our member list * @param m the member to be deleted */ public static void Delete(Member m) { Iterator&lt;Member&gt; it = members.iterator(); while(it.hasNext()) { if(m.equals(it.next())) { it.remove(); } } } /** * Search for a specific member in the member list * @param m the member that needs to be found * @return the reference of the object Member * @throws UserNotFoundExeption whether the member was not found in the list */ public static Member Search (Member m) throws UserNotFoundExeption { Iterator&lt;Member&gt; it = members.iterator(); while(it.hasNext()) { if(m.equals(it.next())) { return it.next(); }else{ UserNotFoundExeption ex = new UserNotFoundExeption(it.next().getUsername()); throw ex; } } return null; } /** * The login method enables checking whether the login was made successfully or not. if not, it can throw two * exceptions to handle the errors * @param member * @return * @throws UserNotFoundExeption * @throws FailedLoginException */ public static boolean login (Member m) throws UserNotFoundExeption,FailedLoginException { try{ Member member = Search(m); if (!m.authenticate(member.getPassword())) { FailedLoginException ex2 = new FailedLoginException (member.getPassword()); throw ex2; } else { return true; } }catch(UserNotFoundExeption ex){ throw ex; } } /** * this behavior modify attributes of the corresponding class * @param &lt;T&gt; this generic helps to accept any type of parameter change, hence we can change any type * @param m this is the member that need to change his information * @param choice the choice of which information to change * @param change the new change on the member attribute * @throws UserNotFoundExeption */ public static &lt;T&gt; void Modify(Member m, int choice, T change) throws UserNotFoundExeption { try{ Member member = Search(m); switch(choice) { case 1: member.setUsername((String)change); break; case 2: member.setPassword((String)change); break; case 3: member.setCommunity((Community)change); break; } }catch(UserNotFoundExeption ex){ throw ex; } } /** * display the member list objects information */ public static void Display() { Iterator&lt;Member&gt; it = members.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } /** * Sort objects in the list */ public static void Sort() { Iterator&lt;Member&gt; it = members.iterator(); Member[] Members_Array = members.toArray(new Member[members.size()]); Member temp; for(int i = 0; i&lt;members.size(); i++) { for(int j = 0; j &lt; members.size() - (i+1); j++) { if(Members_Array[j].compareTo(Members_Array[j+1]) &gt; 0) { temp = Members_Array[j]; Members_Array[j] = Members_Array[j+1]; Members_Array[j+1] = temp; } } } } } </code></pre> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload