如何将我的数组从单行更改为一个20×20的方阵?(Java)

huangapple go评论126阅读模式
英文:

How do i change my array from being on 1 line to being a 20x20 square? (Java)

问题

public class MyGrid {

    public static void main(String[] args) throws IOException {
        FileReader file = new FileReader("list.txt");
        int[][] grid = new int[20][20];
        int row = 0;
        int col = 0;

        try {
            Scanner input = new Scanner(file);
            while (input.hasNext()) {
                grid[row][col] = input.nextInt();
                col++;

                if (col == 20) {
                    col = 0;
                    row++;
                }
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (int[] rowArray : grid) {
            System.out.println(Arrays.toString(rowArray));
        }
    }
}
英文:

I need to change how my array is formatted to where it shows as a 20x20 square. Any ideas on best way to do this?

public class MyGrid {

public static void main(String[] args) throws IOException
{
    FileReader file = new FileReader("list.txt");
    int[] integers = new int [400];
    int i=0;
    try {
        Scanner input = new Scanner(file);
        while(input.hasNext())
        {
            integers[i] = input.nextInt();
            i++;
        }
        input.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    System.out.println(Arrays.toString(integers));
}

}

答案1

得分: 1

try-with-resources 语句 很不错;我建议充分利用它来进行安全的清理。我认为不需要使用 FileReader,只用 File 就足够了。然后,每当有 20 个值时,打印一个换行,否则打印一个空格;然后打印该值。代码如下:

int[] integers = new int[400];
try (Scanner input = new Scanner(new File("list.txt"))) {
    int i = 0;
    while (input.hasNextInt()) {
        integers[i] = input.nextInt();
        if (i != 0) {
            if (i % 20 == 0) {
                System.out.println();
            } else {
                System.out.print(" ");
            }
        }
        System.out.printf("%03d", integers[i]);
        i++;
    }
} catch (Exception e) {
    e.printStackTrace();
}
英文:

The try-with-resources statement is nice; I suggest taking advantage of it to clean-up safely. I don't see any need for a FileReader with your Scanner (File is good enough). Then every 20 values you print a newline - otherwise print a space; then print the value. Like,

int[] integers = new int[400];
try (Scanner input = new Scanner(new File("list.txt"))) {
	int i = 0;
	while (input.hasNextInt()) {
		integers[i] = input.nextInt();
		if (i != 0) {
			if (i % 20 == 0) {
				System.out.println();
			} else {
				System.out.print(" ");
			}
		}
		System.out.printf("%03d", integers[i]);
		i++;
	}
} catch (Exception e) {
	e.printStackTrace();
}

答案2

得分: 0

以下是翻译好的内容:

最简单且最快速的方法对我来说

public class MyGrid {
    public static void main(String[] args) throws IOException {
        FileReader file = new FileReader("list.txt");

        int[] integers = new int[400];
        int[][] table = new int[20][20];
        int m, n, i = 0;

        int tableWidth = table[0].length; // 在这种情况下为 20

        try {
            Scanner input = new Scanner(file);
            while(input.hasNext()) {
                int value = input.nextInt();
                integers[i] = value;

                m = i / tableWidth; // 行索引
                n = i % tableWidth; // 列索引
                table[m][n] = value;

                i++;
            }

            input.close();
        } catch(Exception e) {
            e.printStackTrace();
        }

        System.out.println(Arrays.toString(integers));
    }
}

此外此代码将适应任何其他表格大小例如 500600 或 4237 个元素)。

**注意** 此代码将会在一个二维数组中**存储数据**但不会在控制台中显示如果您想要在读取文件时显示数据我建议您查看适应性更强的 **@Elliott Frisch** 的答案
英文:

The simplest and fastest way to do this (for me) would be that:

public class MyGrid {
    public static void main(String[] args) throws IOException {
        FileReader file = new FileReader("list.txt");

        int[] integers = new int[400];
        int[][] table = new int[20][20];
        int m, n, i = 0;

        int tableWidth = table[0].length; // 20 in that case

        try {
            Scanner input = new Scanner(file);
            while(input.hasNext()) {
                int value = input.nextInt();
                integers[i] = value;
                
                m = i / tableWidth; // Row index
                n = i % tableWidth; // Column index
                table[m][n] = value;

                i++;
            }

            input.close();
        } catch(Exception e) {
            e.printStackTrace();
        }

        System.out.println(Arrays.toString(integers));
    }
}

Moreover, this code will adapt to any other table size (e.g. 500, 600 or 4237 elements).

CAUTION: this code will store data in a 2D array but it will not display it in the console. If you want to display data while reading file, I suggest you to look at @Elliott Frisch answer which is more adapted.

huangapple
  • 本文由 发表于 2020年9月29日 07:26:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64110896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定