在Java中,将元素分离为整数数组的文件生成的ArrayList转换为整数数组
我对Java中的文件操作非常陌生。我有一个由如下数字组成的文件 六, 1 2 13 我试图把所有这些数字放在一个整数数组中。我找到了一个准备好的代码,将它们放在字符串列表中,但现在它对我来说并不是无用的。我想做的是将它们存储在一个int数组中,并逐个索引地访问它们。我想要int[]NumbersFromFile={6,1,2,1,3}; 这是我的密码在Java中,将元素分离为整数数组的文件生成的ArrayList转换为整数数组,java,arrays,list,file,arraylist,Java,Arrays,List,File,Arraylist,我对Java中的文件操作非常陌生。我有一个由如下数字组成的文件 六, 1 2 13 我试图把所有这些数字放在一个整数数组中。我找到了一个准备好的代码,将它们放在字符串列表中,但现在它对我来说并不是无用的。我想做的是将它们存储在一个int数组中,并逐个索引地访问它们。我想要int[]NumbersFromFile={6,1,2,1,3}; 这是我的密码 public String[] ReadNumbersFromFile(String name) throws FileNotFoundExce
public String[] ReadNumbersFromFile(String name) throws FileNotFoundException {
String token1 = "";
Scanner inFile1 = new Scanner(new File(name)).useDelimiter(",\\s*");
ArrayList<String> temps = new ArrayList<String>();
// while loop
while (inFile1.hasNext()) {
// find next line
token1 = inFile1.next();
temps.add(token1);
}
inFile1.close();
String[] tempsArray = new String[temps.size()];
tempsArray = temps.toArray(tempsArray);
for (String s : tempsArray)
{
System.out.print(s);
}
return tempsArray;
}
如何从该文件获取int数组?
提前感谢。更改此行:
ArrayList<String> temps = new ArrayList<String>();
token1 = inFile1.next();
为此:
ArrayList<Integer> temps = new ArrayList<Integer>();
token1 = inFile1.nextInt();
或者,可以编写for循环,使用Integer.parseIntyourInt将ArrayList解析为int[]
循环遍历arrayList并添加到和int[],而不是字符串[],如下所示:
public int[] ReadNumbersFromFile(String name) throws FileNotFoundException {
//String to store each number from the file
String token1 = "";
//Open the file and create a scanner to read it
Scanner inFile1 = new Scanner(new File(name));
//temporary arrayList to store what we read from the file
//scanners return strings
ArrayList<String> temps = new ArrayList<String>();
// while the scanner can see it has more to read
while (inFile1.hasNext()) {
// save the number read
token1 = inFile1.next();
//add it to the arrayList
temps.add(token1);
}
//close the scanner when done using to free up that resource
inFile1.close();
//create the standard int array that is the same length as the arrayList<String>
int[] tempsArray = new int[temps.size()];
//loop through the arrayList<String>
//this is what contains each number from the file, just as Strings
for(int i = 0; i < temps.size(); i++) {
//Integar.parseInt(String s) takes a string of numbers and returns it as int
//save this to our int[]
tempsArray[i] = Integer.parseInt(temps.get(i));
}
//return the int[]
return tempsArray;
}
@frenchDolphin它存储tempsArray[0]处的所有数字,解析不起作用的原因是我尝试了它。我怎么处理这个案子?另一个答案是@B.U frenchDolphin's。试试我的代码,看看它是否有效。我对它进行了编辑,因为您不需要更改分隔符。非常感谢您,它现在可以工作了。你能解释一下背后的逻辑吗@MrMadsen@B.U是的,我编辑了这篇文章,在每一行之前都有一条评论解释它在做什么。如果你还有关于某一行的问题,请告诉我,我会在这里回答。
public int[] ReadNumbersFromFile(String name) throws FileNotFoundException {
//String to store each number from the file
String token1 = "";
//Open the file and create a scanner to read it
Scanner inFile1 = new Scanner(new File(name));
//temporary arrayList to store what we read from the file
//scanners return strings
ArrayList<String> temps = new ArrayList<String>();
// while the scanner can see it has more to read
while (inFile1.hasNext()) {
// save the number read
token1 = inFile1.next();
//add it to the arrayList
temps.add(token1);
}
//close the scanner when done using to free up that resource
inFile1.close();
//create the standard int array that is the same length as the arrayList<String>
int[] tempsArray = new int[temps.size()];
//loop through the arrayList<String>
//this is what contains each number from the file, just as Strings
for(int i = 0; i < temps.size(); i++) {
//Integar.parseInt(String s) takes a string of numbers and returns it as int
//save this to our int[]
tempsArray[i] = Integer.parseInt(temps.get(i));
}
//return the int[]
return tempsArray;
}