什么是创建和填充50个阵列的最快方法?
我需要编写一个Java代码来创建和填充50个数组(Array1到Array50,每个数组有10000000行和3列):
Create Array 1 (1至10000000,1至3)并填充它使用文件 1 .txt;
创建数组 2 (1至10000000,1至3),并用文件 2 .txt填充它;
[...]
创建数组 49 (1至1000000,1至3),并用文件填充 49 .txt;
创建数组 50 (1至1000000,1至3),并用文件 50 .txt填充它;
实现这一目标的最快方法是什么?是否可以使用循环以使其不需要太多的代码行?
谢谢。
编辑1: 对不起,我以前不清楚。
实际上,我需要创建50个int数组,并用相应文件中的元素填充每个数组。
创建Int Array 1 用10000000行和3列创建,并用文件 1 .txt中的元素填充它;
创建Int Array 2 用10000000行和3列创建,并用文件 2 .txt中的元素填充它;
我正在使用此代码来创建和填充50个阵列中的每个阵列:
int[][] array1 = new int [10000000][3];
List<List<String>> records = new ArrayList<>();
try (BufferedReader br = new BufferedReader(filepath/File1.txt"))) {
String line;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
records.add(Arrays.asList(values));
}
}
由于代码太多,我正在尝试获得一个可以更快且线条更少的新代码。我已经尝试使用一系列数组。我写了这件代码,但是它不起作用(有人可以纠正吗?):
String arrayA[] = {"array1", "array2", "array3", "array4", "array5", "array6", "array7", "array8", "array9", "array10", "array11", "array12", "array13", "array14", "array15", "array16", "array17", "array18", "array19", "array20", "array21", "array22", "array23", "array24", "array25", "array26", "array27", "array28", "array29", "array30", "array31", "array32", "array33", "array34", "array35", "array36", "array37", "array38", "array39", "array40", "array41", "array42", "array43", "array44", "array45", "array46", "array47", "array48", "array49", "array50"};
for (int i = 0; i < arrayA.length; i++) {
int[][] arrayA[i] = new int [10000000][3];
List<List<String>> records = new ArrayList<>();
try (BufferedReader br = new BufferedReader("C:/filepath/File" + i + 1 + ".txt"))) {
String line;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
records.add(Arrays.asList(values));
}
}
System.out.println(arrayA[i][1][3]);
}
I need to write a java code to create and populate 50 arrays (array1 to array50, with 10000000 rows and 3 columns each array):
create array1(1 To 10000000, 1 To 3) and populate it with file1.txt;
create array2(1 To 10000000, 1 To 3) and populate it with file2.txt;
[...]
create array49(1 To 1000000, 1 To 3) and populate it with file49.txt;
create array50(1 To 1000000, 1 To 3) and populate it with file50.txt;
What's the fastest way to accomplish this goal? Is it possible to use a loop so that it doesn't take so many lines of code?
Thanks.
Edit 1:
I'm sorry I was not clear previously.
In fact, I need to create 50 int arrays and fill each one with elements in the respective file.
Create int array1 with 10000000 rows and 3 columns and populate it with elements in File1.txt;
Create int array2 with 10000000 rows and 3 columns and populate it with elements in File2.txt;
I'm using this piece of code to create and populate each one of the 50 arrays:
int[][] array1 = new int [10000000][3];
List<List<String>> records = new ArrayList<>();
try (BufferedReader br = new BufferedReader(filepath/File1.txt"))) {
String line;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
records.add(Arrays.asList(values));
}
}
As there are too many lines of code, I am trying to get a new code that could be faster and with less lines. I have already tryied to use an array of arrays. I wrote this piece of code, but it does not work (could someone please correct it?):
String arrayA[] = {"array1", "array2", "array3", "array4", "array5", "array6", "array7", "array8", "array9", "array10", "array11", "array12", "array13", "array14", "array15", "array16", "array17", "array18", "array19", "array20", "array21", "array22", "array23", "array24", "array25", "array26", "array27", "array28", "array29", "array30", "array31", "array32", "array33", "array34", "array35", "array36", "array37", "array38", "array39", "array40", "array41", "array42", "array43", "array44", "array45", "array46", "array47", "array48", "array49", "array50"};
for (int i = 0; i < arrayA.length; i++) {
int[][] arrayA[i] = new int [10000000][3];
List<List<String>> records = new ArrayList<>();
try (BufferedReader br = new BufferedReader("C:/filepath/File" + i + 1 + ".txt"))) {
String line;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
records.add(Arrays.asList(values));
}
}
System.out.println(arrayA[i][1][3]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能有一种方法可以更快地做到这一点,但这就是我想到的:
There may be a way to do it faster, but this is what I came up with: