【发布时间】:2013-03-29 13:45:49
【问题描述】:
我有以下代码。但是我怀疑它是否是正确的实现方式。
我的意思是:在集合框架中有许多数据结构可以使用,创建类 (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;
}
}
}
}
}
谢谢!
【问题讨论】:
-
只是出于好奇:为什么您的班级中的所有内容都是静态的?
-
更重要的是:不幸的是,排序和修改不能很好地结合在一起。因此,答案需要考虑更多使用的内容:您是否经常查询您的列表?你是每秒修改一次还是更多?
-
.Delete()(.remove())、.Sort()(Collections.sort()) 和 .Search()(.indexOf(),.contains()) 通常已经实现并且在大多数情况下比任何单独的实现更有效。主题:正如 mabi 已经说过的,决定你最需要什么(90% 的情况)并使用适合你需要的 List 类型。
-
另外,请遵守 Java 命名约定。方法和变量以小写字母开头,不包含下划线,并且是驼峰式。
-
冒泡排序是你所拥有的吗?! O_o
标签: java data-structures collections arraylist