2

我有以下代码。但是,我怀疑这是否是实施它的正确方法。

我的意思是:在集合框架中,有许多数据结构可供使用,创建类 ( MemberList) 来管理许多成员的聚合可以使用ArrayList, LinkedList, 优先级队列 ...

我想使用适合我需要的数据结构,并且在搜索、排序、删除、添加、修改和删除方面具有最小的 Big O 可能。

public class MemberList{
    /**
     *a list of accounts existing in the database
     */
    private static List<Member> members = new ArrayList<Member>();
    /**
     * 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<Member> 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<Member> 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 <T> 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 <T> 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<Member> it = members.iterator();

        while(it.hasNext())
        {
            System.out.println(it.next());
        }
    }

    /**
     * Sort objects in the list
     */
    public static void Sort()
    {
        Iterator<Member> it = members.iterator();
        Member[] Members_Array = members.toArray(new Member[members.size()]);
        Member temp;

        for(int i = 0; i<members.size(); i++)
        {
            for(int j = 0; j < members.size() - (i+1); j++)
            {
                if(Members_Array[j].compareTo(Members_Array[j+1]) > 0)
                {
                    temp = Members_Array[j];
                    Members_Array[j] = Members_Array[j+1];
                    Members_Array[j+1] = temp;
                }
            }
        }

    }    
}

谢谢!

4

2 回答 2

1

这个问题太笼统了,“Java中正确使用集合的方法”也是一个哲学问题,所以无法科学回答。

具体到您的情况,根据您的成员池,您可能不想在需要拉出成员时对其进行迭代。我建议您使用类似 a 的东西HashMap<String,Member>,其中每个成员都有一个可识别的唯一键(例如用户名)。这将授予您 O(1) 的访问速度,并允许您在需要时使用.values().

您可以像这样使用 HashMap:

// This is how you create a hash map:
HashMap<String,Member> members = new HashMap<String,Member>();

// This is how you add an object to it. It is slower than lists,
// but since reading happens far often, it pays off.
members.put("ben", new Member());

// This is how you access an object in the hash map.
// Accessing a hash map is O(1).
Member member = members.get("ben");

// This is how you remove an object from the hash map.
// Removing an object is also O(1)
members.remove("ben");

// Hash maps are also iterable
for(Member member : members.values()) {
}
于 2013-04-07T19:00:30.957 回答
0

如果您不想使用 JDBC,我会使用数组列表。但是以后如果您的项目要增长,您必须使用 JDBC。

于 2013-04-07T18:53:28.510 回答