我是Java的新手。我有一个数组,其中包含int
类型的体育ID。如果你运动1小时,这些身份证有运动名称和卡路里燃烧。我想通过这个数组的元素添加这些属性(名称和卡路里)。我还在阅读包含体育ID,姓名和卡路里信息的文件。我按\t
拆分它们。它是分裂但问题是,我的实现没有分配属性。我需要在Sports class
而不是Main class
上执行此操作。我必须在其他类中访问此数组及其属性。如果我在致电array[2].name
时给出一个例子,那么必须给我Basketball
。或者,array[2].calorie
必须给我200
。我试过for循环并将它们分配为:
for ( int i = 0; i < array.length; i++ ) {
array[i].name = nameOfSport;
array[i].calorie = calorieOfSport;
}
import java.io.*;
import java.util.*;
public class Sport {
private int sportID;
private String sportName;
private int sportCalorie;
public int[] sportArray = new int[0];
public void readFileSport() {
File file = new File("src/sport.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
int i = 0;
String str;
try {
while ((str = br.readLine()) != null) {
int j = 0;
for ( String retval: str.split("\t")) {
if ( j == 0 ) {
System.out.println(retval);
sportArray = addElementArray(sportArray, Integer.parseInt(retval));
System.out.println(sportArray[i]);
j++;
} else {
if (j == 1) {
// System.out.println(retval);
setSportName(sportArray, retval, i);
System.out.println(sportArray[i].sportName);
j++;
} else if (j == 2) {
//System.out.println(retval);
setSportCalorie(Integer.parseInt(retval));
j++;
}
}
}
i++;
}
} catch (FileNotFoundException e) {
System.out.println("Sport File could not find");
}
} catch (IOException e) {
System.out.println("Sport file could not read!");
}
}
public void assignFields () {
}
public void setSportID (int sportID) {
this.sportID = sportID;
}
public void setSportName (int[] array, String name, int e) {
array[e].sportName = name;
}
public void setSportCalorie (int sportCalorie) {
this.sportCalorie = sportCalorie;
}
public void print() {
int k = 0;
while ( k < sportArray.length ) {
System.out.println(sportID);
k++;
}
}
public static int[] addElementArray(int[] array, int newInt) {
int[] newArray = new int[array.length + 1];
for ( int i = 0 ; i < array.length ; i++ ) {
newArray[i] = array[i];
}
newArray[newArray.length - 1] = newInt;
return newArray;
}
public int[] getSportArray() {
return sportArray;
}
}
答案 0 :(得分:0)
因此,假设您有一个名为Sports
的课程
public class Sport{
public String name;
public int calorie;
}
然后用
启动它们Sport[] sports = new Sport[] // or your own split() to specify length
Arrays.setAll(sports, i->new Sport())
这应该能够解决问题。
您还可以使用ArrayList
,您可以在循环中添加新元素。
public static void readFileSport() {
File file = new File("src/sport.txt");
try {
Scanner br = new Scanner(file);
int i = 0;
String str;
try {
while (br.hasNextLine()) {
String[] info=str.split("\t");
System.out.println(info[0]);
sportArray = addElementArray(sportArray, Integer.parseInt(info[0]));
System.out.println(sportArray[i]);
// System.out.println(info[1]);
setSportName(sportArray, info[1], i);
System.out.println(sportArray[i].sportName);
//System.out.println(info[2]);
setSportCalorie(Integer.parseInt(info[2]));
i++;
}
} catch (FileNotFoundException e) {
System.out.println("Sport File could not find");
}
} catch (IOException e) {
System.out.println("Sport file could not read!");
}
}
如果每行只有3个对象,实际上这种方式更容易。