java - 我想通过在其类中定义的变量按字母顺序排列对象

标签 java object arraylist alphabetized

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

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()));
}

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

哦,这是类(class):主题。

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;
}

}

最佳答案

您可以使用自己的比较器将主题 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);
        }
    }); 

关于java - 我想通过在其类中定义的变量按字母顺序排列对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19782958/

相关文章:

java - 如何将文件保存在内存中并读取文件输出流?

java - QtWebkit 多线程

java - IntellijIDEA 在哪里可以缓存 Maven 依赖项?

javascript - 未捕获的类型错误 : Cannot read property 'player' of undefined

java - 将数组列表更改为列表列表java

java - 在 JavaFX 中动态加载图像到 Anchorpane

javascript - 在使用它之前,我是否必须在 Javascript 中的对象内部声明一个空数组?

java - 将对象转换为字节数组,反之亦然,无需序列化

java - java中ArrayList的lastIndexOf运行时间

java - 将来自不同数组列表类的数据与不同实例合并