从输入文件中显示学生记录

时间:2016-03-13 18:55:02

标签: java arrays

我无法读取输入文件中的学生记录并将其添加到现有数组中。

原始数组大小为10.当数组已满时,其大小将加倍。

如果输入文件包含25条记录,则我的数组仅显示最后5条记录。我知道我的阵列扩展编码不正确,我无法解决。这是我的课程:

import java.util.*;

public class ClassPeriod {
   private int              myNumStudents;
   private Student[]        myStudents;
   private String           myClassName;
   int N = 10;

   public ClassPeriod(String classname){
       myClassName = classname;
       myNumStudents = 0;
       myStudents = new Student[N];
   }

   // add the Student to the myStudents array. If the array is full, create a new
   // one twice the size of the current one. Update myNumStudents accordingly.
   public void addStudent(Student st){
        for (int i=0; i<1; i++){
            if (myNumStudents == 10 || myNumStudents == 20) {//student array size is 10, if 10 is reached, double its size
                N = 2*myNumStudents;
                myStudents = new Student[N];
            }
            switch (myNumStudents)
            {
                case 0: myStudents[0] = st; break;
                ...
                ...
                case 24: myStudents[24] = st; break;
                default: break; 
            }
            myNumStudents++;//increment myNumStudents by 1
        }
        for (int j=0;j<N;j++){
            System.out.println("Students: " + myStudents[j]);
        }
    }

    public Student[] getStudents(){
        System.out.println("myNumStudents: " + myNumStudents);
        Student temp[] = new Student[myNumStudents];
        for (int i=0; i<myNumStudents; i++){
            temp[i] = myStudents[i];
        }
        System.out.println("temp: " + temp.length);
        return temp;
    }

    public String toString(){
        String s = new String(myClassName + "\n");
        int i;
        for (i=0; i<myNumStudents-1; i++)
            s += myStudents[i].toString() + "\n";
        s += myStudents[myNumStudents-1];
        return s;
    }
}

import chn.util.*;
import java.util.Properties;
import java.util.Enumeration;
import java.util.*;

public class ArrayRecordsSortSearchApplication {

    private static final String STUDENT_FILENAME = "students25.txt";

    public static void main(String args[]) {
        Student[] sortedStudents = null;
        ClassPeriod p1 = new ClassPeriod("PERIOD 1");
        readClass(p1);
        ConsoleIO console = new ConsoleIO();
        char choice;

        do {
            showMenu();
            choice = console.readLine().charAt(0);
            System.out.println();
            switch (choice) {
            case '1':
                showStudents(p1.getStudents());
            case '2':
                break;
            default:
                System.out.println("That's not a choice");
                break;
            }
        } while (choice != '2');
    }

    public static void showMenu() {
        System.out.println();
        System.out.println("1)  Show students in original order");
        System.out.println("2)  Quit?");
        System.out.print("choice: ");
    }

    public static void showStudents(Student[] studs){
        System.out.print("studs.length: " +studs.length +"\n");
        for (int i=0; i<studs.length; i++)
           System.out.println(studs[i]); 
    }

    public static void readClass(ClassPeriod p1){
        System.out.println("Please wait while data file loads...");
        FileInput infile = new FileInput(STUDENT_FILENAME);

        do {
            int id = infile.readInt();
            double gpa = infile.readDouble();
            String name = infile.readLine();
            Student s = new Student(name,id,gpa);
            p1.addStudent(s);
        } while ( infile.hasMoreLines() );

        infile.close();
    }
}

1 个答案:

答案 0 :(得分:1)

此赋值看起来像ArrayList的实现。一旦超出旧数组的大小,就需要创建新数组的双倍大小,并将旧数组的元素复制到一个新数组中,然后添加要插入的新元素。

在您的代码中:

if (myNumStudents == 10 || myNumStudents == 20) {//student array size is 10, if 10 is reached, double its size
                N = 2*myNumStudents;
                myStudents = new Student[N];
                ....somewhere here you should actually copy all the elements from the old array as first elements of the new array and then insert the new element to be inserted in array. 
}