1.Java : :
Student [] Stu = New Student [3]; for (int i = 0; i <3; i ++) {Stu [i] = new Student ();}2. 否则会提示空指针异常
projet de package; import java.io. *; import java.util.scanner; classe étudiante {private int id; nom de chaîne privé; Score INT privé; public void setid (int id) {this.id = id; } public int getID () {return this.id; } public void setName (string name) {this.name = name; } public String getName () {return this.name; } public void setScore (int score) {this.score = score; } public int getScore () {return this.score; }} classe publique Project2 {fichier file = new File ("e: /data.txt"); FileWriter FileWrite = NULL; BufferedWriter write = null; FileReader fileread = null; BufferedReader Read = null; Étudiant [] Stu = nouvel étudiant [3]; public void put () {try {fileWrite = new FileWriter (fichier); } catch (ioException e) {// TODO 自动生成的 Catch 块 E.PrintStackTrace (); } write = new BufferedWriter (FileWrite); pour (int i = 0; i <3; i ++) {System.out.println ("请输入第" + (i + 1) + "个学生的 id, 姓名, 成绩:"); Scanner dans = nouveau scanner (System.in); try {String str = in.nextline (); String data [] = str.split (""); pour (int j = 0; j <3; j ++) {write.write (data [j]); write.newline (); }} catch (ioException e) {// TODO 自动生成的 Catch 块 E.PrintStackTrace (); }} essayez {write.close (); filewrite.close (); } catch (ioException e) {// TODO 自动生成的 Catch 块 E.PrintStackTrace (); }} public void get () {int sum = 0; Double Ave; try {fileRead = new FileReader (fichier); } catch (filenotfoundException e) {// too 自动生成的 catch 块 e.printStackTrace (); } read = new BufferedReader (fileread); pour (int i = 0; i <3; i ++) {Stu [i] = new Student (); essayez {Stu [i] .setid (Integer.ParseInt (read.readline ())); Stu [i] .setName (read.readline ()); Stu [i] .setsCore (Integer.ParseInt (read.readline ())); } catch (exception e) {// TODO 自动生成的 Catch 块 E.PrintStackTrace (); }} pour (int i = 0; i <3; i ++) {sum + = stu [i] .getScore (); } ave = sum * 1.0 / 3; System.out.println ("学生的平均成绩为 :" + Ave); essayez {read.close (); fileRead.close (); } catch (ioException e) {// TODO 自动生成的 Catch 块 E.PrintStackTrace (); }} public static void main (String [] args) {project2 pro = new project2 (); pro.put (); pro.get (); }}:
这样我们就可以在项目当中, 根据项目需求自己来定义想要的数组.