2013-11-05 1 views
1

나는 Experiment라는 수퍼 클래스와 Subject의 서브 클래스를 가진 Group의 서브 클래스로 프로그램을 만들고있다. printFinalExperiment를 누를 때 그룹 이름과 그룹 내에 포함 된 모든 주제를 인쇄하고 싶지만 모든 주제를 인쇄 할 때마다 그 이유를 알아낼 수 없습니다. 나는 단지 자바를 배우는 중이다. 그리고 이것은 아마도 비효율적 일 것이다. 그러나 나는이 문제를 해결하기 위해 여러 유형의리스트를 실험 해왔다.하지만 무엇을 해야할지 알 수 없다. 또한 별도의 arraylist를 만드는 대신에 arraylist를 알파벳순으로 배열하는 것이 더 쉬운 방법일까요? 실험 수업에서 알파벳 화 방법으로 볼 수 있습니다.arraylists 안에 들어있는 arraylists에 액세스하는 데 문제가 있습니다. 나는 생각한다. 나는 100 % 확실하지 않다.

// 클래스 실험

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

}

,

답변

1

이것은 Group 클래스에서 ArrayList 그룹을 선언 한 방식과 관련이있을 수 있습니다.

static으로 신고 한 이유가 있습니까? 비 정적 인스턴스 변수는 클래스의 각 인스턴스 (이 경우 각 그룹 객체)가 자체 그룹 ArrayList를 갖게됨을 의미합니다. 반면에 정적 인스턴스 변수는 일반적인 의미에서 클래스와 관련된 변수입니다. 인스턴스화하는 각 Group 객체에는 자신 만의 ArrayList 그룹이 없습니다.

그것에게 비 정적 인스턴스 변수를 만드는 시도

또한

, 여기에 더 나은 정적 및 자바에서 비 정적 사이의 차이를 설명 할 수있는 링크입니다 (단순히 정적 단어를 삭제.) : http://www.caveofprogramming.com/articles/java/java-for-beginners-static-variables-what-are-they/

+0

감사를 너 너무 많이 soooooooo. 그것은 효과가 있었다. 나는 그것에 대해 완전히 잊었다. 제 동급생에게 정적 변수를 초기화하기 위해 정적 변수를 어떻게 사용할 것인지를 보여주기 위해 arraylist에 추가했습니다. 그러나 그것을 가져가는 것을 잊어 버렸습니다. –

+0

문제 없습니다. 기꺼이 도와주세요. 한가지 더 - 확실한 이유가 있는지 확인하십시오 특정 인스턴스 변수를'public'으로 선언하십시오. 클래스에 액세스 할 수있는 사람이 실제로 의도하지 않는 한 모든 인스턴스 변수를 수정할 수 있기를 원하지 않을 것입니다. 더 자주 그런 다음, 당신은 그들을 '비공개'로 선언하고 클래스의 사용자가 적합하다고 생각하는 방식으로 만 액세스 할 수 있도록 메소드를 정의해야합니다. –

관련 문제