java 創(chuàng)建自定義數(shù)組
1.java創(chuàng)建自定義類數(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)目需求自己來定義想要的數(shù)組.
相關(guān)文章
java中的Integer的toBinaryString()方法實(shí)例
這篇文章主要介紹了java中的Integer的toBinaryString()方法實(shí)例,有需要的朋友可以參考一下2013-12-12
Springboot如何使用filter對(duì)request body參數(shù)進(jìn)行校驗(yàn)
這篇文章主要介紹了Springboot如何使用filter對(duì)request body參數(shù)進(jìn)行校驗(yàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
使用AOP攔截Controller獲取@PathVariable注解傳入的參數(shù)
這篇文章主要介紹了使用AOP攔截Controller獲取@PathVariable注解傳入的參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring?Cloud?中使用?Sentinel?實(shí)現(xiàn)服務(wù)限流的兩種方式
這篇文章主要介紹了Spring?Cloud?中使用?Sentinel?實(shí)現(xiàn)服務(wù)限流的方式,通過示例代碼主要介紹了Sentinel的兩種實(shí)現(xiàn)限流的方式,需要的朋友可以參考下2024-03-03
用SpringBoot Admin監(jiān)控SpringBoot程序
這篇文章主要介紹了用SpringBoot Admin監(jiān)控SpringBoot程序,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-10-10
SpringBoot?項(xiàng)目的創(chuàng)建與啟動(dòng)步驟詳解
這篇文章主要介紹了SpringBoot?項(xiàng)目的創(chuàng)建與啟動(dòng),本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
Spring Cloud之遠(yuǎn)程調(diào)用OpenFeign參數(shù)傳遞
本文介紹了Spring Cloud中使用OpenFeign進(jìn)行遠(yuǎn)程調(diào)用時(shí),參數(shù)傳遞的不同方式,包括傳遞單個(gè)參數(shù)、多個(gè)參數(shù)、對(duì)象和JSON數(shù)據(jù),感興的朋友一起看看吧2025-03-03

