Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用集合的正确方法?_Java_Data Structures_Collections_Arraylist - Fatal编程技术网

在Java中使用集合的正确方法?

在Java中使用集合的正确方法?,java,data-structures,collections,arraylist,Java,Data Structures,Collections,Arraylist,我有以下代码。然而,我怀疑这是否是实施它的正确方式 我的意思是:在集合框架中有许多数据结构可以使用和创建类(MemberList)来管理许多成员的聚合,可以使用ArrayList、LinkedList、优先级队列 我希望使用一种适合我的需要的数据结构,并且在搜索、排序、删除、添加、修改和删除时具有尽可能小的O public class MemberList{ /** *a list of accounts existing in the database */

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

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

我希望使用一种适合我的需要的数据结构,并且在搜索、排序、删除、添加、修改和删除时具有尽可能小的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;
                }
            }
        }

    }    
}
公共类成员列表{
/**
*数据库中现有帐户的列表
*/
私有静态列表成员=新ArrayList();
/**
*将成员添加到我们的成员列表中
*@param m要添加的成员
*/
公共静态无效添加(成员m)
{
成员.添加(m);
/**
*从我们的会员名单中删除一名会员
*@param m要删除的成员
*/
公共静态无效删除(成员m)
{
Iterator it=members.Iterator();
while(it.hasNext())
{
如果(m.equals(it.next()))
{
it.remove();
}
}
}
/**
*在成员列表中搜索特定成员
*@param m需要找到的成员
*@返回对象成员的引用
*@throws UserNotFoundExeption是否在列表中找不到该成员
*/
公共静态成员搜索(成员m)抛出UserNotFoundException
{
Iterator it=members.Iterator();
while(it.hasNext())
{
如果(m.equals(it.next()))
{
返回它。下一步();
}否则{
usernotfoundexoption ex=新的usernotfoundexoption(it.next().getUsername());
掷骰子;
}
}
返回null;
}
/**
*login方法允许检查登录是否成功。如果不成功,它可以抛出两个
*处理错误的异常
*@param成员
*@返回
*@UserNotFoundExeption
*@Trows FailedLoginException
*/
公共静态布尔登录(成员m)
抛出UserNotFoundException、FailedLoginException{
试一试{
成员=搜索(m);
如果(!m.authenticate(member.getPassword()))
{
FailedLoginException ex2=新的FailedLoginException(member.getPassword());
抛出ex2;
}
其他的
{
返回true;
}
}捕获(UserNotFoundExeption ex){
掷骰子;
}
}
/**
*此行为会修改相应类的属性
*@param此泛型有助于接受任何类型的参数更改,因此我们可以更改任何类型
*@param m m这是需要更改其信息的成员
*@param choice选择要更改的信息
*@param更改成员属性上的新更改
*@UserNotFoundExeption
*/
publicstaticvoidmodify(成员m、int-choice、T-change)抛出UserNotFoundExeption
{
试一试{
成员=搜索(m);
开关(选择)
{
案例1:
member.setUsername((字符串)更改);
打破
案例2:
member.setPassword((字符串)更改);
打破
案例3:
成员。setCommunity((社区)变更);
打破
}
}捕获(UserNotFoundExeption ex){
掷骰子;
}
}
/**
*显示成员列表对象信息
*/
公共静态无效显示()
{
Iterator it=members.Iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
/**
*对列表中的对象进行排序
*/
公共静态void排序()
{
Iterator it=members.Iterator();
Member[]Members_Array=Members.toArray(新成员[Members.size()]);
会员临时工;
对于(int i=0;i 0)
{
temp=成员_数组[j];
成员数组[j]=成员数组[j+1];
成员_数组[j+1]=temp;
}
}
}
}    
}

谢谢大家!

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

这个问题太广泛了,而且“在Java中使用集合的正确方法”也是一个哲学问题,因此无法科学地回答

具体到您的情况,根据您的成员池,您可能不希望在需要提取成员时对其进行迭代。我建议您使用类似于
HashMap
,其中每个成员都有一个可识别的唯一密钥(例如用户名)。这将授予您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()) {
}
//这就是创建哈希映射的方法:
HashMap成员=新的HashMap();
//这就是向其中添加对象的方式。它比列表慢,
//但由于阅读经常发生,所以它是值得的。
成员。put(“本”,新成员());
//这是访问哈希映射中对象的方式。
//访问哈希映射是O(1)。
Member=members.get(“ben”);
//这就是如何从哈希映射中删除对象。
//移除对象也是O(1)
成员。移除(“本”);
//散列映射也是可编辑的
对于(成员:members.values()){
}

出于好奇:为什么类中的所有内容都是静态的?更重要的是:不幸的是,排序和修改不能很好地结合在一起。因此,答案需要考虑使用什么