简体   繁体   English

如何将数组列表打印到JTextArea?

[英]How to do I print an arraylist to a JTextArea?

I can't seem to figure out how to print an arrayList<String> to a JTextArea and have tried using both append() and setText() . 我似乎无法弄清楚如何将arrayList<String>打印到JTextArea,并尝试使用append()setText() I have also tried to create a method which prints out the ArrayList through a loop, but it can't be added to the JTextArea because it is not of type String. 我也试图创建一个通过循环打印出ArrayList的方法,但是由于它不是String类型,所以无法将其添加到JTextArea中。

An applicant is supposed to take a student profile (name, grade, university selections) and add it to the ArrayList<String> Applicants. 申请人应该获取学生资料(姓名,年级,大学选择)并将其添加到ArrayList<String>申请人中。 This is done through a JButton if it holds true for the following if statement: 如果对于以下if语句为true,则可以通过JButton完成此操作:

if (studentsAverage > 74 && validInput && studentsAverage < 100) {

    studentChoices.addAll(uniOptions.getSelectedValuesList());
    person = new Student (namePromptTF.getText(), averagePromptTF.getText(),Applicants, studentChoices);
    arrayCount++;
    numberOfApplicants.setText(arrayCount +"/" +100+"students");                    

    person.printProfile(); //dont need
    person.studentProfileSort(); // dont need


    displayAllApplicants.append(person.returnProfile());

    Applicants.add(person);

The array is passed to a Student object that holds: 该数组传递给一个拥有以下内容的Student对象:

private ArrayList<Student> ApplicantArray;

ApplicantArray is then sorted through this method: 然后通过以下方法对ApplicantArray进行排序:

void studentProfileSort() {
    Student profileLine = null;

    int numberOfStudents = ApplicantArray.size();   
    ArrayList<Student> displayAllSorted = new ArrayList<Student>();

    for(int i = 1; i<numberOfStudents - 1; i++){
        for(int j = 0; j<(numberOfStudents - i); j++) {

            if(ApplicantArray.get(i).getFamilyName().compareTo(ApplicantArray.get(i).getFamilyName())>0){
                ApplicantArray.set(j, ApplicantArray.get(i));
            }
        }

        ApplicantArray.get(i).returnProfile();          
    }
}

Is there a way to have a return statement inside of a loop so that I can change my method to a String type? 有没有一种方法可以在循环内包含return语句,以便可以将我的方法更改为String类型?

At first your sorting algorithm does not seem to work 起初,您的排序算法似乎无效

ApplicantArray.get(i).getFamilyName().compareTo(ApplicantArray.get(i).getFamilyName())

You compare the value with it self and this results always in 0. Even if this would work, in the next line you override the array by setting a value rather than swapping the two values or setting to a new ArrayList. 您将值与其自身进行比较,结果始终为0。即使可行,在下一行中,您也可以通过设置一个值而不是交换两个值或将其设置为新的ArrayList来覆盖数组。

But if everything works, this is how you could print those students: 但是,如果一切正常,您可以通过以下方式打印这些学生:

StringBuilder b = new StringBuilder();
for (Student student : applicantArray) {
    b.append(student + "\n"); // this if you implemented toString() in Student
    b.append(student.getFamilyName() + ' ' + student.getFirstName() + "\n"); // or something like this
}
textArea.setText(b.toString());

PS: you should never use UpperCamelCase for variables or parameters, use lowerCamelCase instead (eg ApplicantArray -> applicantArray) PS:永远不要对变量或参数使用UpperCamelCase,而应使用LowerCamelCase(例如ApplicantArray-> ApplicantArray)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM