最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java 創(chuàng)建自定義數(shù)組

 更新時(shí)間:2016年07月07日 12:01:53   投稿:lqh  
本篇文章是關(guān)于java 如何自己創(chuàng)建自定義數(shù)組,這里給大家一個(gè)小實(shí)例,希望能幫助有所需要的同學(xué)

1.java創(chuàng)建自定義類(lèi)數(shù)組方法:

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

2.否則會(huì)提示空指針異常

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 自動(dòng)生成的 catch 塊
      e.printStackTrace();
    }
    write = new BufferedWriter(filewrite);
    for(int i = 0; i < 3; i ++)
    {
      System.out.println("請(qǐng)輸入第" + (i + 1) + "個(gè)學(xué)生的ID,姓名,成績(jī):");
      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 自動(dòng)生成的 catch 塊
        e.printStackTrace();
      }
       
    }
    try {
      write.close();
      filewrite.close();
    } catch (IOException e) {
      // TODO 自動(dòng)生成的 catch 塊
      e.printStackTrace();
    }
  }
   
   
  public void get()
  {
    int sum = 0;
    double ave;
    try {
      fileread = new FileReader(file);
    } catch (FileNotFoundException e) {
      // TODO 自動(dòng)生成的 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 自動(dòng)生成的 catch 塊
        e.printStackTrace();
      }
    }
     
    for(int i = 0; i < 3; i ++)
    {
      sum += stu[i].getScore();
    }
    ave = sum * 1.0/3;
    System.out.println("學(xué)生的平均成績(jī)?yōu)椋? + ave);
    try {
      read.close();
      fileread.close();
    } catch (IOException e) {
      // TODO 自動(dòng)生成的 catch 塊
      e.printStackTrace();
    }
  }
  public static void main (String []args)
  {
    project2 pro = new project2();
    pro.put();
    pro.get();
  }
}

    總結(jié):

             這樣我們就可以在項(xiàng)目當(dāng)中,根據(jù)項(xiàng)目需求自己來(lái)定義想要的數(shù)組.

相關(guān)文章

最新評(píng)論

绵竹市| 大关县| 封开县| 芮城县| 微博| 富顺县| 鄢陵县| 东丽区| 贵港市| 潞城市| 雷州市| 且末县| 固镇县| 花莲市| 山阴县| 上高县| 霍城县| 建湖县| 远安县| 新晃| 浮山县| 新巴尔虎左旗| 津市市| 贵阳市| 苍溪县| 宁南县| 九寨沟县| 峨山| 阿图什市| 郎溪县| 张家界市| 子长县| 贵南县| 阳江市| 织金县| 巨野县| 兴化市| 板桥市| 黄石市| 沙河市| 丰城市|