我想从txt文件中读取并使用System.arraycopy
进行复制
parts[]
至scores[]
。
我知道scores
必须是String
才能工作。我的
构造函数必须是int[]
的{{1}}数组。因此scores
是scores
。有没有解决方案?
int[]
这是我的构造函数:
private void processLine(String line) {
String mygoden1 = "";
//String mygoden2 = "";
try {
String parts [] = line.split(",");
mygoden1= parts[0];
Name name = new Name(parts[1]);
int gN1=Integer.parseInt(mygoden1);
String gL= parts[3];
//gL = gL.trim();
String gC = parts[4];
gC=gC.trim();
int scoresLength = parts.length-4;
String scores[]=new String[scoresLength];
System.arraycopy(parts,4,scores, 0, scoresLength);
Gamer g1 = new Gamer(gN1 ,name,scores, gL, gC);
this.add(g1);
}
答案 0 :(得分:1)
当您需要转换数据时,请勿使用System.arraycopy
。迭代每个元素的parts
和parseInt()
,然后将其存储在scores
。
答案 1 :(得分:0)
我会替换
String scores[]=new String[scoresLength];
System.arraycopy(parts,4,scores, 0, scoresLength);
与
int[] scores = new int[scoresLength];
for(int i = 0; i < scoresLength; i++)
scores[i] = Integer.parseInt(parts[i + 4]);