java 创建自定义数组

 更新时间:2016年07月07日 12:01:53   投稿:lqh  
本篇文章是关于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中的Integer的toBinaryString()方法实例

    java中的Integer的toBinaryString()方法实例

    这篇文章主要介绍了java中的Integer的toBinaryString()方法实例,有需要的朋友可以参考一下
    2013-12-12
  • SpringBoot底层注解详解

    SpringBoot底层注解详解

    这篇文章主要介绍了SpringBoot底层注解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2023-05-05
  • Springboot如何使用filter对request body参数进行校验

    Springboot如何使用filter对request body参数进行校验

    这篇文章主要介绍了Springboot如何使用filter对request body参数进行校验,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Java Spring的refresh方法你知道吗

    Java Spring的refresh方法你知道吗

    这篇文章主要为大家详细介绍了Java Spring的refresh方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • Java 入门图形用户界面设计之单选按钮

    Java 入门图形用户界面设计之单选按钮

    图形界面(简称GUI)是指采用图形方式显示的计算机操作用户界面。与早期计算机使用的命令行界面相比,图形界面对于用户来说在视觉上更易于接受,本篇精讲Java语言中关于图形用户界面的单选按钮
    2022-02-02
  • 使用AOP拦截Controller获取@PathVariable注解传入的参数

    使用AOP拦截Controller获取@PathVariable注解传入的参数

    这篇文章主要介绍了使用AOP拦截Controller获取@PathVariable注解传入的参数,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Spring Cloud 中使用 Sentinel 实现服务限流的两种方式

    Spring Cloud 中使用 Sentinel 实现服务限流的两种方式

    这篇文章主要介绍了Spring Cloud 中使用 Sentinel 实现服务限流的方式,通过示例代码主要介绍了Sentinel的两种实现限流的方式,需要的朋友可以参考下
    2024-03-03
  • 用SpringBoot Admin监控SpringBoot程序

    用SpringBoot Admin监控SpringBoot程序

    这篇文章主要介绍了用SpringBoot Admin监控SpringBoot程序,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下
    2020-10-10
  • SpringBoot 项目的创建与启动步骤详解

    SpringBoot 项目的创建与启动步骤详解

    这篇文章主要介绍了SpringBoot 项目的创建与启动,本文分步骤给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • Spring Cloud之远程调用OpenFeign参数传递

    Spring Cloud之远程调用OpenFeign参数传递

    本文介绍了Spring Cloud中使用OpenFeign进行远程调用时,参数传递的不同方式,包括传递单个参数、多个参数、对象和JSON数据,感兴的朋友一起看看吧
    2025-03-03

最新评论