将 JList 分为 2 组的优化方法

发布于 2024-11-10 05:44:02 字数 2141 浏览 1 评论 0原文

我有一个用户列表。其中一些用户处于第一状态,而其他用户处于第二状态。所以我想要的是将这个列表显示为首先,它按排序顺序显示存在 = 1 的用户,然后按排序顺序显示存在 = 2 的用户。这里的排序是根据用户名进行的。目前我可以做所有这些事情,但是需要很长时间才能完成,因为列表中有大约 250 个用户。而且用户的存在可以随时更改。我有一个套接字连接来监听它,那时我还必须首先显示列表上的所有内容来显示更新的用户数据。如何才能以更少的时间完成此操作并且不会使我的应用程序挂起?

这是我目前正在做的事情:

    List<User> us = new ArrayList<User>();
    int num = model[j].getSize();
    String[] strArr = new String[num];
    for (int i = 0; i < num; i++) {
        strArr[i] = ((User)model[j].get(i)).getName();
        if(!isDuplicateSortedUser(strArr[i], us))
            us.add((User)model[j].get(i));
    }
    sortArray(Collator.getInstance(), strArr);
    User user;
    List<User> temp2 = new ArrayList<User>();
    List<User> temp1 = new ArrayList<User>();
    for (String string : strArr) {
        for (int i = 0; i < num; i++) {
            user = (User) us.get(i);
            if(user.getName().equals(string)){
                if(!isDuplicateSortedUser(user.getUserid(), temp2) && !temp2.contains(user) && !temp1.contains(user)){
                    if(user.getPresence().toLowerCase().equals("1st"))
                        temp2.add(user);
                    else
                        temp1.add(user);
                }
            }
        }
    }
    int l=0;
    for (User user2 : temp1) {
        model[j].setElementAt(user2, l);
        l++;
    }
    for (User user2 : temp2) {
        model[j].setElementAt(user2, l);
        l++;
    }

这里 modelJListDefaultListModel。 sortArray 方法是:

private void sortArray(Collator collator, String[] strArray) {
        String tmp;
        if (strArray.length == 1) return;
        for (int i = 0; i < strArray.length; i++) {
            for (int j = i + 1; j < strArray.length; j++) {
                if(collator.compare(strArray[i], strArray[j] ) > 0 ) {
                    tmp = strArray[i];
                    strArray[i] = strArray[j];
                    strArray[j] = tmp;
                }
            }
        } 
    }

如何优化上面的代码?

I have a jlist of users. In which some user has presence 1st and while others have 2nd. SO what I want is to display this list as First it shows users with presence=1st in sorted order and then user with presence=2 in sorted order. Here sorting is done based on user's name. Currently I'm able to do all this things but it takes a long to do it as the list has many users around 250. Also the presence of a user can change any time. I have a socket connection to listen to that and that time I also have to above all things on list to show updated user data. How can I do this in a way that takes less time and don't make my application hang?

Here is what I am doing currently:

    List<User> us = new ArrayList<User>();
    int num = model[j].getSize();
    String[] strArr = new String[num];
    for (int i = 0; i < num; i++) {
        strArr[i] = ((User)model[j].get(i)).getName();
        if(!isDuplicateSortedUser(strArr[i], us))
            us.add((User)model[j].get(i));
    }
    sortArray(Collator.getInstance(), strArr);
    User user;
    List<User> temp2 = new ArrayList<User>();
    List<User> temp1 = new ArrayList<User>();
    for (String string : strArr) {
        for (int i = 0; i < num; i++) {
            user = (User) us.get(i);
            if(user.getName().equals(string)){
                if(!isDuplicateSortedUser(user.getUserid(), temp2) && !temp2.contains(user) && !temp1.contains(user)){
                    if(user.getPresence().toLowerCase().equals("1st"))
                        temp2.add(user);
                    else
                        temp1.add(user);
                }
            }
        }
    }
    int l=0;
    for (User user2 : temp1) {
        model[j].setElementAt(user2, l);
        l++;
    }
    for (User user2 : temp2) {
        model[j].setElementAt(user2, l);
        l++;
    }

Here model is the DefaultListModel of JList. The sortArray method is:

private void sortArray(Collator collator, String[] strArray) {
        String tmp;
        if (strArray.length == 1) return;
        for (int i = 0; i < strArray.length; i++) {
            for (int j = i + 1; j < strArray.length; j++) {
                if(collator.compare(strArray[i], strArray[j] ) > 0 ) {
                    tmp = strArray[i];
                    strArray[i] = strArray[j];
                    strArray[j] = tmp;
                }
            }
        } 
    }

How can I optimize above code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑梦风尘 2024-11-17 05:44:02

将用户放入 ArrayList 中并使用内置的 Java 排序功能: http://download.oracle.com/javase/6/docs/api/java/util/Collections.html

这将调用合并排序,这将比您编写的更有效。

您到底想在第一个 for 循环中做什么?

据我所知,您只需要执行以下操作:

  1. 对所有用户循环一次,将所有“第一个”用户扔到ArrayList A中,将所有“第二”用户扔到ArrayList B中
  2. 排序A,排序B,
  3. 将A和B合并回来一起。

Put the users in an ArrayList and use the built-in Java sort functionality: http://download.oracle.com/javase/6/docs/api/java/util/Collections.html

This will invoke a mergesort, which will be more efficient than what you've written.

What exactly are you trying to do in the first for loop?

As far as I can see, you only need to do the following:

  1. Loop once over all users, throwing all "1st" users in ArrayList A, all "2nd" users in ArrayList B
  2. Sort A, Sort B,
  3. Merge A and B back together.
幽蝶幻影 2024-11-17 05:44:02

正如 @Jeroen 所指出的,实现 Comparable 是首选方法。您还可以实现Comparator,如RecordComparator 或使用 SortedComboBoxModel

As noted by @Jeroen, implementing Comparable is the preferred approach. You can also implement Comparator as shown in RecordComparator or use SortedComboBoxModel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文