繁体   English   中英

在Java中使用动态大小初始化二维字符串数组

[英]initialize two dimensional string array with dynamic size in java

我的记录数未知,我需要将所有记录放入字符串二维数组中。

我不知道记录的数量,因此,不知道字符串2d数组初始化所需的行数和列数。

目前我正在使用如下:

String[][] data = new String[100][100]; 

在这里,我对行和列的数量进行了硬编码,但需要在字符串2d数组中允许的动态大小。 任何建议请!

Rgrds

您可以使用以下类将数据存储在HashMap ,并将其转换为二维字符串数组。

public class ArrayStructure {
    private HashMap<Point, String> map = new HashMap<Point, String>();
    private int maxRow = 0;
    private int maxColumn = 0;

    public ArrayStructure() {
    }

    public void add(int row, int column, String string) {
        map.put(new Point(row, column), string);
        maxRow = Math.max(row, maxRow);
        maxColumn = Math.max(column, maxColumn);
    }

    public String[][] toArray() {
        String[][] result = new String[maxRow + 1][maxColumn + 1];
        for (int row = 0; row <= maxRow; ++row)
            for (int column = 0; column <= maxColumn; ++column) {
                Point p = new Point(row, column);
                result[row][column] = map.containsKey(p) ? map.get(p) : "";
            }
        return result;
    }
}

范例程式码

public static void main(String[] args) throws IOException {
    ArrayStructure s = new ArrayStructure();
    s.add(0, 0, "1");
    s.add(1, 1, "4");

    String[][] data = s.toArray();
    for (int i = 0; i < data.length; ++i) {
        for (int j = 0; j < data[i].length; ++j)
            System.out.print(data[i][j] + " ");
        System.out.println();
    }
}

输出量

1  
 4 

您可以临时将它们存储在List<String[]>并使用List#toArray(String[])将其转换为二维数组。

public static void main(String[] args) throws IOException {
    BufferedReader r = new BufferedReader(new FileReader(new File(
            "data.txt")));

    String line;
    List<String[]> list = new ArrayList<String[]>();

    while ((line = r.readLine()) != null)
        list.add(line.split(" +"));

    String[][] data = new String[list.size()][];
    list.toArray(data);

    for (int i = 0; i < data.length; ++i) {
        for (int j = 0; j < data[i].length; ++j)
            System.out.print(data[i][j]+" ");
        System.out.println();
    }
    r.close();
}

数据文件

1 2 3 4 5
2 5 3
2  5  5 8

输出量

1 2 3 4 5
2 5 3
2 5 5 8

您可以简单地使用文字,空的二维数组进行初始化:

String[][] data = new String[][]{{}}

这应该工作:

public static void main(String args[]) throws IOException {
    // create the object
    String[][] data;

    // ----- dinamically know the matrix dimension ----- //
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    int r = Integer.parseInt(bufferedReader.readLine());
    int c = Integer.parseInt(bufferedReader.readLine());
    // ------------------------------------------------ //

    // allocate the object
    data = new String[r][c];

    // init the object
    for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++)
            data[i][j] = "hello";
}

在此示例中,您知道矩阵维运行时,可通过控制台手动指定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM