我正在制作一个程序,其中包含一个名为 Experiment 的超类和一个 Group 的子类以及一个 Subject 的子类。当我点击 printFinalExperiment 时,我希望它打印出组名和组中包含的所有主题,但是每当我尝试打印出所有主题时,我都无法弄清楚为什么。我只是在学习java,这可能效率很低,但我一直在尝试不同类型的列表来尝试解决这个问题,但我不知道该怎么做。还有一种更简单的方法可以按名称按字母顺序排列我的主题数组列表,而不必创建单独的字符串数组列表?见于实验课按字母排序法。
//类实验
import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class Experiment
{
public Random number;
public ArrayList<String> allSubject;
public ArrayList<Subject> allSubjects,alphaSubjects,matched;
public ArrayList<Group> experiment;
public int value,groups;
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(int numberOfGroups)
{
groups = numberOfGroups;
number = new Random();
numbers = new ArrayList<Integer>();
experiment = new ArrayList<Group>();
matched = new ArrayList<Subject>();
allSubjects = new ArrayList<Subject>();
allSubject = new ArrayList<String>();
alphaSubjects = new ArrayList<Subject>();
for(int i = 0; i < numberOfGroups; i++)
{
experiment.add(new Group(i));
}
}
/**
* Input the number of desired subjects and groups
* for you experiment. This will create the number
* of groups specified and will assign the subjects
* as close to even as possible.
*/
public Experiment(int numberOfSubjects, int numberOfGroups)
{
number = new Random();
numbers = new ArrayList<Integer>();
experiment = new ArrayList<Group>();
matched = new ArrayList<Subject>();
allSubjects = new ArrayList<Subject>();
allSubject = new ArrayList<String>();
alphaSubjects = new ArrayList<Subject>();
for(int i=0;i<numberOfSubjects;i++)
{
addDefaultSubject(i);
}
assignGroups(numberOfGroups);
printDefaultExper();
}
/**
* Adds a new Subject to the experiment.
*/
public void addSubject(String name, String description)
{
allSubjects.add(new Subject(name,description));
allSubject.add(name.toLowerCase());
}
/**
*Used only for the second constructor.
*/
private void addDefaultSubject(int i)
{
allSubjects.add(new Subject(i));
allSubject.add(allSubjects.get(i).getName().toLowerCase());
}
/**
* 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()
{
alphaSubjects.clear();
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()));
}
}
}
}
/**
* Creates random numbers from 0 to
* the number of Subjects in the experiment.
*/
private void randomize()
{
alphabetize();
value = number.nextInt(allSubjects.size());
for(int i = 0; i < allSubjects.size();i++)
{
while(numbers.contains(value))
{
value = number.nextInt(allSubjects.size());
}
numbers.add(value);
}
}
/**
* Assigns the numbers created randomly by
* Blue Jay's random number generator to the
* alphabetized list Subjects.
*/
public void assignNumbers()
{
matched.clear();
numbers.clear();
randomize();
for(int i =0; i < numbers.size();i++)
{
matched.add(alphaSubjects.get(numbers.get(i)));
experiment.get(i%groups).addSubject(matched.get(i));
}
}
//The previous method and the next method are what i was trouble shooting so either one can be changed.
/**
* Splits subjects into groups. Every nth (n is the number of
* groups input) is assign to a new group. For example:
* Say you have 17 subjects (0-16) and you want 4 groups.
* Subjects 0,4,8,12, and 16 will be in group 1, subjects
* 1,5,9,and 13 will be in group 2 and so on until 4 complete
* groups are made.
*/
public void assignGroups(int numberOfGroups)
{
numbers.clear();
assignNumbers();
if(numberOfGroups <=0)
{
System.out.println("You need at least one group.");
}
else{
int numberOfSubjects = allSubjects.size();
experiment = new ArrayList<Group>();
for(int i = 0; i < numberOfGroups; i++)
{
Group group = new Group(i);
for(Integer j = i; j < matched.size(); j+=numberOfGroups)
{
group.addSubjects(matched.get(j).getName(),matched.get(j).getDescription());
}
experiment.add(group);
}
}
}
/**
* Prints the final layout of the Groups with its
* subjects in alphabetical order
*/
public void printFinalExperiment()
{
for(Group group: experiment)
{
System.out.println("Group " + group.getGroupName());
group.printGroupList();
}
System.out.println();
}
//this next method is only used for the second constructor.
private void printDefaultExper()
{
for(Group group: experiment)
{
System.out.println("Group " + group.getGroupName());
System.out.println(" " + group.getSize());
}
System.out.println();
}
//类组
import java.util.ArrayList;
public class Group
{
// instance variables - replace the example below with your own
public static ArrayList<Subject> group;
public int groupNumber;
public int size;
public String groupName;
public Group(int groupNumber)
{
this.groupNumber = groupNumber;
groupName = "Group" + groupNumber;
group = new ArrayList<Subject>();
}
public Group(String groupName)
{
this.groupName=groupName;
group = new ArrayList<Subject>();
}
public void addSubject(Subject subject)
{
group.add(subject);
}
public void addSubjects(String name,String description)
{
group.add(new Subject(name,description));
}
public int getGroupNumber()
{
return groupNumber;
}
public int getSize()
{
size = group.size();
return size;
}
public String getGroupName()
{
return groupName;
}
public void printGroupList()
{
for(Subject subject: group)
{
System.out.println(" " + subject.getName() + ": " + subject.getDescription());
}
}
}
//类主题
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;
}
}