如何将 parseInt 数组传递给上一个数组?
How to pass parseInt array to previous array?
有人要求我读取文件并将其中的文本转换为二维数组。由于 Eclipse 很麻烦并且不会 open/read 我的文本文件,我在 class 中使用单独初始化的二维数组进行了测试。我的问题是我不知道将 parseInt'ed 数组放回新数组。或者如何使用它来形成一个新的二维数组。这是我的代码:
public static void main(String[] args) {
String[][] tester = new String[][] { { "-1 2 3 0" }, { "-1 3 4 0" }, { "-1 3 -4 0" }, { "-1 -3 4 0" } };
int row = 0;
int col = 0;
int count = 0;
int[][] formula = new int[4][];
while (count < 4) {
String temp = tester[row][col];
String[] charArray = temp.split("\s+");
int[] line = new int[charArray.length];
for (int i = 0; i < charArray.length; i++) {
String numAsStr = charArray[i];
line[i] = Integer.parseInt(numAsStr);
//what to do here??
}
row++;
count++;
}
System.out.println(Arrays.deepToString(formula).replace("], ",
"]\n"));
}
}
我想生成一个如下所示的数组:
-1 2 3 0
-1 3 4 0
-1 3 -4 0
-1 -3 4 0
我怎样才能做到这一点?
您必须将项目添加到数组 formula
。只需在此处添加 formula[count] = line;
:
for (int i = 0; i < charArray.length; i++) {
String numAsStr = charArray[i];
line[i] = Integer.parseInt(numAsStr);
formula[count] = line;
}
输出为:
[[-1, 2, 3, 0]
[-1, 3, 4, 0]
[-1, 3, -4, 0]
[-1, -3, 4, 0]]
Select 如果有效,作为答案!
像这样更改 formula
的定义:
int[][] formula = new int[tester.length][];
您希望公式的行数与测试仪的行数相同,对吗?
同时将 while
循环更改为循环直到 tester.length
而不是常量 4:
while (counter < tester.length)
现在,在 for
循环之后才是真正的业务开始的地方:
for (int i = 0; i < charArray.length; i++) {
String numAsStr = charArray[i];
line[i] = Integer.parseInt(numAsStr);
}
formula[row] = line; // <------------
在 for 循环中,您已经解析了测试仪一行中的所有整数。现在是时候将整数行放入 formula
了,不是吗?
How to read a large text file line by line using Java?
-1 2 3 0
-1 3 4 0
-1 3 -4 0
-1 -3 4 0
演示
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import java.io.IOException;
public class MyFile {
public static void main(String[] args) throws IOException {
File file = new File("myFile.txt"); // read file name
Scanner scan = null;
int[][] myArray = new int[4][4]; // how many?
String line = null; // line in .txt
String[] token = null; // number token
try {
scan = new Scanner(file);
while (scan.hasNextLine()) {
for (int i = 0; i < 4; i++) { // loop thru row
line = scan.nextLine(); // scan line
token = line.split(" ");
for (int j = 0; j < 4; j++) {
myArray[i][j] = Integer.parseInt(token[j]);// loop thru col & parse token int
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
scan.close();
System.out.println(Arrays.deepToString(myArray)); //fix this
}
}
在 for
循环
下添加 formula[row] = line;
我建议您使用一个列表,您可以在其中存储数组,然后它可以轻松转换为 int[][]...
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String test = "-1 2 3 0\n-1 3 4 0\n-1 3 -4 0\n-1 -3 4 0";
Scanner sc = new Scanner(test);
//read
List<int[]> arrayList = new ArrayList<>();
while (sc.hasNextLine()) {
arrayList.add(Arrays.stream(sc.nextLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray());
}
//print
arrayList.forEach(arr -> {
Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
System.out.println();
});
//convert
int[][] matrix = new int[arrayList.size()][];
matrix = arrayList.toArray(matrix);
//print
Arrays.stream(matrix).forEach(arr -> {
Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
System.out.println();
});
}
}
请注意,如果您必须将其拆分为多个正则表达式模式,则必须为每一行使用平面图。
有人要求我读取文件并将其中的文本转换为二维数组。由于 Eclipse 很麻烦并且不会 open/read 我的文本文件,我在 class 中使用单独初始化的二维数组进行了测试。我的问题是我不知道将 parseInt'ed 数组放回新数组。或者如何使用它来形成一个新的二维数组。这是我的代码: public static void main(String[] args) {
String[][] tester = new String[][] { { "-1 2 3 0" }, { "-1 3 4 0" }, { "-1 3 -4 0" }, { "-1 -3 4 0" } };
int row = 0;
int col = 0;
int count = 0;
int[][] formula = new int[4][];
while (count < 4) {
String temp = tester[row][col];
String[] charArray = temp.split("\s+");
int[] line = new int[charArray.length];
for (int i = 0; i < charArray.length; i++) {
String numAsStr = charArray[i];
line[i] = Integer.parseInt(numAsStr);
//what to do here??
}
row++;
count++;
}
System.out.println(Arrays.deepToString(formula).replace("], ",
"]\n"));
}
}
我想生成一个如下所示的数组:
-1 2 3 0
-1 3 4 0
-1 3 -4 0
-1 -3 4 0
我怎样才能做到这一点?
您必须将项目添加到数组 formula
。只需在此处添加 formula[count] = line;
:
for (int i = 0; i < charArray.length; i++) {
String numAsStr = charArray[i];
line[i] = Integer.parseInt(numAsStr);
formula[count] = line;
}
输出为:
[[-1, 2, 3, 0]
[-1, 3, 4, 0]
[-1, 3, -4, 0]
[-1, -3, 4, 0]]
Select 如果有效,作为答案!
像这样更改 formula
的定义:
int[][] formula = new int[tester.length][];
您希望公式的行数与测试仪的行数相同,对吗?
同时将 while
循环更改为循环直到 tester.length
而不是常量 4:
while (counter < tester.length)
现在,在 for
循环之后才是真正的业务开始的地方:
for (int i = 0; i < charArray.length; i++) {
String numAsStr = charArray[i];
line[i] = Integer.parseInt(numAsStr);
}
formula[row] = line; // <------------
在 for 循环中,您已经解析了测试仪一行中的所有整数。现在是时候将整数行放入 formula
了,不是吗?
How to read a large text file line by line using Java?
-1 2 3 0
-1 3 4 0
-1 3 -4 0
-1 -3 4 0
演示
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import java.io.IOException;
public class MyFile {
public static void main(String[] args) throws IOException {
File file = new File("myFile.txt"); // read file name
Scanner scan = null;
int[][] myArray = new int[4][4]; // how many?
String line = null; // line in .txt
String[] token = null; // number token
try {
scan = new Scanner(file);
while (scan.hasNextLine()) {
for (int i = 0; i < 4; i++) { // loop thru row
line = scan.nextLine(); // scan line
token = line.split(" ");
for (int j = 0; j < 4; j++) {
myArray[i][j] = Integer.parseInt(token[j]);// loop thru col & parse token int
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
scan.close();
System.out.println(Arrays.deepToString(myArray)); //fix this
}
}
在 for
循环
formula[row] = line;
我建议您使用一个列表,您可以在其中存储数组,然后它可以轻松转换为 int[][]...
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String test = "-1 2 3 0\n-1 3 4 0\n-1 3 -4 0\n-1 -3 4 0";
Scanner sc = new Scanner(test);
//read
List<int[]> arrayList = new ArrayList<>();
while (sc.hasNextLine()) {
arrayList.add(Arrays.stream(sc.nextLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray());
}
//print
arrayList.forEach(arr -> {
Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
System.out.println();
});
//convert
int[][] matrix = new int[arrayList.size()][];
matrix = arrayList.toArray(matrix);
//print
Arrays.stream(matrix).forEach(arr -> {
Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
System.out.println();
});
}
}
请注意,如果您必须将其拆分为多个正则表达式模式,则必须为每一行使用平面图。