欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)建自定義類數(shù)組方法:

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) + "個(gè)學(xué)生的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("學(xué)生的平均成績?yōu)椋? + 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();
  }
}

    總結(jié):

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

相關(guān)文章

  • java中注解的原理解析

    java中注解的原理解析

    這篇文章主要介紹了java中注解的原理解析,java 注解又稱 Java 標(biāo)注,是 JDK5.0 引入的一種注釋機(jī)制,可以理解為為某個(gè)東西,打個(gè)標(biāo)記的記號,等要使用這個(gè)注解時(shí),可以通過反射獲取標(biāo)注里面的內(nèi)容,需要的朋友可以參考下
    2023-10-10
  • 深入了解HttpClient的ResponseHandler接口

    深入了解HttpClient的ResponseHandler接口

    這篇文章主要為大家介紹了深入了解HttpClient的ResponseHandler接口,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • idea遠(yuǎn)程debug調(diào)試部署在tomcat上項(xiàng)目

    idea遠(yuǎn)程debug調(diào)試部署在tomcat上項(xiàng)目

    本文主要介紹了idea遠(yuǎn)程debug調(diào)試部署在tomcat上項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 詳解SpringBoot2 使用Spring Session集群

    詳解SpringBoot2 使用Spring Session集群

    這篇文章主要介紹了SpringBoot2 使用Spring Session集群,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-04-04
  • Java實(shí)現(xiàn)讀取csv文件的兩種方式

    Java實(shí)現(xiàn)讀取csv文件的兩種方式

    這篇文章主要為大家詳細(xì)介紹了如何利用Java讀取csv文件的兩種方式,文中的示例代碼講解詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以了解一下
    2023-12-12
  • Mybatis中@Param的用法和作用詳解

    Mybatis中@Param的用法和作用詳解

    這篇文章主要介紹了Mybatis中@Param的用法和作用,在文中給大家補(bǔ)充了spring中@param和mybatis中@param使用區(qū)別,需要的朋友可以參考下
    2017-09-09
  • Java源碼解析之object類

    Java源碼解析之object類

    前些天看到別人討論閱讀源碼有什么用這個(gè)問題,有一句話說的特別好:學(xué)習(xí)別人實(shí)現(xiàn)某個(gè)功能的設(shè)計(jì)思路,來提高自己的編程水平。本文主要介紹了Java源碼解析之object類,需要的朋友可以參考。
    2017-10-10
  • Java中字符數(shù)組、String類、StringBuffer三者之間相互轉(zhuǎn)換

    Java中字符數(shù)組、String類、StringBuffer三者之間相互轉(zhuǎn)換

    這篇文章主要介紹了Java中字符數(shù)組、String類、StringBuffer三者之間相互轉(zhuǎn)換,需要的朋友可以參考下
    2018-05-05
  • MyEclipse去除網(wǎng)上復(fù)制下來的代碼帶有的行號(正則去除行號)

    MyEclipse去除網(wǎng)上復(fù)制下來的代碼帶有的行號(正則去除行號)

    這篇文章主要介紹了MyEclipse去除網(wǎng)上復(fù)制下來的代碼帶有的行號(正則去除行號)的相關(guān)資料,需要的朋友可以參考下
    2017-10-10
  • SpringBoot中如何啟動Tomcat流程

    SpringBoot中如何啟動Tomcat流程

    這篇文章主要介紹了SpringBoot中如何啟動Tomcat流程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-05-05

最新評論