java - 显示输入文件中的学生记录

标签 java arrays

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

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

如果输入文件包含 25 条记录,我的数组仅显示最后 5 条记录。我知道我的数组扩展编码不正确,并且我无法解决。这是我的类(class):

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

最佳答案

这个赋值看起来像 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. 
}

关于java - 显示输入文件中的学生记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35974363/

相关文章:

c - 将十六进制从 .txt 转换为 C 中的数组/字符串

java - 整数数组中的重复数字(Java 基础级别)和时间复杂度

java - 使用 model 属性传递的 Spring MVC 对象为空

java - 从 Excel 坐标放置 anylogic 代理

javascript - 将 JavaScript 对象添加到多个数组

arrays - 使用索引匹配的数组公式

c++ - 在循环中将值传递给数组

java - 如何在java中检索和存储 map 的单个元素

java - 有条件地从流中删除第一个(索引为零)元素

java - Android 应用程序在 setSupportActionBar 崩溃