欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java 创建自定义数组

时间:2021-01-28 10:23:36|栏目:JAVA代码|点击:

1.java创建自定义类数组方法:

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

2.否则会提示空指针异常

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

    总结:

             这样我们就可以在项目当中,根据项目需求自己来定义想要的数组.

上一篇:java多线程实现有序输出ABC

栏    目:JAVA代码

下一篇:在Java的Spring框架中配置Quartz的教程

本文标题:java 创建自定义数组

本文地址:http://www.codeinn.net/misctech/52616.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有