我正在制作一个为我设置实验的程序,我想按字母顺序排列我输入的主题(或人)。我有一个类型主题的arraylist,我想用他们的名字按字母顺序排列。
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()));
}
因此,不必将主题添加到arraylist,然后必须从该主题中删除名称并将其添加到完全不同的arraylist,是否有办法按主题的名称按字母顺序排列。
哦,这里是班级:主题。
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;
}
}
答案 0 :(得分: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);
}
});
答案 1 :(得分: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());
}
}
}
答案 2 :(得分: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
列表进行排序。
这是一个tutorial以获取更多信息。祝你好运!