我正在创建一个名为Experiment的超类程序和一个子类为Subject的子类。当我点击printFinalExperiment时,我希望它打印出组名和组中包含的所有主题,但每当我尝试它打印出所有主题时,我都无法找出原因。我只是在学习java,这可能效率很低,但我一直在尝试使用不同类型的列表来尝试解决这个问题,但我无法弄清楚要做什么。还有一种更简单的方法可以按字母顺序按字母顺序排列主题的主题,而不必创建单独的字符串组合吗?在实验课中用alphabetize方法看。
// class Experiment
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());
}
}
}
// class Subject
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)
这可能与您在Group类中声明ArrayList组的方式有关。
您有什么理由将其声明为static
吗?非静态实例变量意味着您的类的每个实例(在本例中为每个Group对象)都将拥有自己的组ArrayList。另一方面,静态实例变量是一般意义上与类关联的变量。您实例化的每个Group对象在您的情况下都没有自己的组ArrayList。
尝试将其设为非静态实例变量(只需删除单词static。)
此外,这里有一个链接可以更好地解释Java中静态和非静态之间的区别:http://www.caveofprogramming.com/articles/java/java-for-beginners-static-variables-what-are-they/