java 建立自訂數組_java

來源:互聯網
上載者:User

1.java建立自訂類數組方法:

Student []stu = new Student[3];for(int i = 0; i < 3; i ++){stu[i] = new Student();}

2.否則會提示null 指標異常

package project; import java.io.*;import java.util.Scanner;class Student{  private int id;  private String name;  private int score;     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;  }}public class project2 {  File file = new File("E:/data.txt");  FileWriter filewrite = null;  BufferedWriter write = null;  FileReader fileread = null;  BufferedReader read = null;  Student []stu = new Student[3];  public void put()  {    try {      filewrite = new FileWriter(file);    } catch (IOException e) {      // TODO 自動產生的 catch 塊      e.printStackTrace();    }    write = new BufferedWriter(filewrite);    for(int i = 0; i < 3; i ++)    {      System.out.println("請輸入第" + (i + 1) + "個學生的ID,姓名,成績:");      Scanner in = new Scanner(System.in);      try {        String str = in.nextLine();        String data[] = str.split(" ");        for(int j = 0; j < 3; j++)        {          write.write(data[j]);          write.newLine();        }               } catch (IOException e) {        // TODO 自動產生的 catch 塊        e.printStackTrace();      }           }    try {      write.close();      filewrite.close();    } catch (IOException e) {      // TODO 自動產生的 catch 塊      e.printStackTrace();    }  }        public void get()  {    int sum = 0;    double ave;    try {      fileread = new FileReader(file);    } catch (FileNotFoundException e) {      // TODO 自動產生的 catch 塊      e.printStackTrace();    }    read = new BufferedReader(fileread);    for(int i = 0; i < 3; i ++)    {      stu[i] = new Student();      try {        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();      }    }         for(int i = 0; i < 3; i ++)    {      sum += stu[i].getScore();    }    ave = sum * 1.0/3;    System.out.println("學生的平均成績為:" + ave);    try {      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();  }}

    總結:

             這樣我們就可以在項目當中,根據項目需求自己來定義想要的數組.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.