0

我正在制作一个为我设置实验的程序,我想按字母顺序排列我输入的主题(或人)。我有一个主题类型的数组列表,我想按他们的名字按字母顺序排列它们。

import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Experiment
{
public Random number;
public ArrayList<String> allSubject;
public ArrayList<Subject> allSubjects,alphaSubjects;
public ArrayList<Group> experiment;
public Integer value;
public HashMap<Integer,Subject> matched;
private ArrayList<Integer> numbers;
/**
 * Make a new Experiment.  Then use method addSubject to add
 * Subjects to your experiment.  Then call the assignGroups
 * method to assign Subjects to each group.
 */
public Experiment()
{
    number = new Random();
    numbers = new ArrayList<Integer>();
    experiment = new ArrayList<Group>();
    matched = new HashMap<Integer,Subject>();
    allSubjects = new ArrayList<Subject>(); 
    allSubject = new ArrayList<String>();
    alphaSubjects = new ArrayList<Subject>();
}

/**
 * Alphabetizes the list of Subjects based on their
 * name input by the user.  As of right now, this method
 * is case sensitive meaning Strings starting with 
 * capitals will be listed before those without capitals.
 */
private void alphabetize()
{

           Collections.sort(allSubject);

        //compare the String arraylist to the subject arraylist to reset the subject arraylist indeces in alphabetical order.

       for(int i =0;i<allSubject.size();i++)
       {
        String theName = allSubject.get(i);
         for(Subject subject:allSubjects)
        {
          if(subject.getName().toLowerCase().contains(theName))
         {
            alphaSubjects.add(new Subject(subject.getName(),subject.getDescription()));
         }
        }

     }
}
/**
 * Adds a new Subject to the experiment.
 */
public void addSubject(String name, String description)
{
    allSubjects.add(new Subject(name,description));
    allSubject.add((name.toLowerCase()));
}

因此,不必将主题添加到数组列表然后必须从该主题中删除名称并将其添加到完全不同的数组列表中,有没有办法按主题名称按字母顺序排列。

哦,这是课程:主题。

public class Subject
{
public final String name;
public final String description;
public Subject(String name, String description)
{
    this.name = name;
    this.description = description;
}
public Subject(int aNumber)
{
    name = "Subject" + aNumber;
    aNumber++;
    description = "default";
}
public String getName()
{
    return name;
}
public String getDescription()
{
    return description;
}

}

4

3 回答 3

1

您可以使用自己的比较器将主题 ArrayList 与 SortedList(http://www.glazedlists.com/documentation/tutorial-100#TOC-Sorting-Tables-Sorting-Tables )包装起来。

SortedList sortedSubjects = new SortedList<Subject>(allSubjects,new Comparator<Subject>() {
        @Override
        public int compare(Subject left, Subject right) {
            return left.getName().compareTo(right.getName);
        }
    }); 
于 2013-11-05T06:32:09.013 回答
0

您可以根据您的说服力实现 Comparator 或 Comparable 并覆盖 compare(..) 、 compareTo(..) 方法。在您的情况下,您需要在实施此方法时考虑“主题名称”。然后Collections.sort(yourList<Subject>)将根据主题名称为您提供排序结果。

    import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class Subject implements Comparable<Subject>
{
public final String name;
public final String description;
public Subject(String name, String description)
{
    this.name = name;
    this.description = description;
}
public Subject(int aNumber)
{
    name = "Subject" + aNumber;
    aNumber++;
    description = "default";
}
public String getName()
{
    return name;
}
public String getDescription()
{
    return description;
}

    @Override
    public int compareTo(Subject o) {
        return this.getName().toUpperCase().compareTo(((Subject)o).getName());
    }

    public static void main(String[] args) {
        Subject s3 = new Subject("C","");
        Subject s1 = new Subject("Z","");
        Subject s2 = new Subject("A","");

        List<Subject> list = new ArrayList<Subject>();
        list.add(s1);
        list.add(s2);
        list.add(s3);

        Collections.sort(list);

        for(Subject sub:list){
            System.out.println(sub.getName());
        }
    }
}
于 2013-11-05T06:25:43.737 回答
0

您需要做的就是让您的类实现Comparable并添加compareTo()

public class Subject implements Comparable<Subject>
{
    ....

     @Override
     public int compareTo(Subject other)
     {
         return this.getName().compareTo(other.getName());
     }
}

现在,由于您的类实现了 Comparable 本身,您可以使用它Collections.sort()来对Subjects.

这是有关更多信息的教程。祝你好运!

于 2013-11-05T06:29:04.993 回答