什么是创建和填充50个阵列的最快方法?

发布于 2025-02-11 15:56:43 字数 2256 浏览 0 评论 0原文

我需要编写一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏雪无痕 2025-02-18 15:56:44

可能有一种方法可以更快地做到这一点,但这就是我想到的:

public static String[][] generateArray(int rows, int cols, String value) {
    String[][] array = new String[rows][cols];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            array[i][j] = value;
        }
    }
    return array;
}

public static void main(String[] args) {
    final int numArrays = 50;
    final int rows = 1000000;
    final int cols = 3;

    List<String[][]> arrays = Collections.synchronizedList(new ArrayList<>(numArrays));
    ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    final CountDownLatch latch = new CountDownLatch(numArrays);

    long start = System.nanoTime();
    for (int i = 0; i < numArrays; i++) {
        final String value = String.format("file%d.txt", i);
        Runnable runnable = new Runnable() {
            public void run() {
                String[][] array = generateArray(rows, cols, value);
                try {
                    arrays.add(array);
                } finally {
                    latch.countDown();
                }
            }
        };
        executor.submit(runnable);
    }
    executor.shutdown();
    try {
        latch.await();
    } catch (Exception e) {
        e.printStackTrace();
    }
    long stop = System.nanoTime();
    long seconds = TimeUnit.NANOSECONDS.toSeconds((stop - start));
    System.out.println("Completed in " + seconds + " seconds.");
}

There may be a way to do it faster, but this is what I came up with:

public static String[][] generateArray(int rows, int cols, String value) {
    String[][] array = new String[rows][cols];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            array[i][j] = value;
        }
    }
    return array;
}

public static void main(String[] args) {
    final int numArrays = 50;
    final int rows = 1000000;
    final int cols = 3;

    List<String[][]> arrays = Collections.synchronizedList(new ArrayList<>(numArrays));
    ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    final CountDownLatch latch = new CountDownLatch(numArrays);

    long start = System.nanoTime();
    for (int i = 0; i < numArrays; i++) {
        final String value = String.format("file%d.txt", i);
        Runnable runnable = new Runnable() {
            public void run() {
                String[][] array = generateArray(rows, cols, value);
                try {
                    arrays.add(array);
                } finally {
                    latch.countDown();
                }
            }
        };
        executor.submit(runnable);
    }
    executor.shutdown();
    try {
        latch.await();
    } catch (Exception e) {
        e.printStackTrace();
    }
    long stop = System.nanoTime();
    long seconds = TimeUnit.NANOSECONDS.toSeconds((stop - start));
    System.out.println("Completed in " + seconds + " seconds.");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文