java - 链接列表中的多维数组不起作用?

标签 java arrays file

所以我有这个代码:

    public static void main (String[] args) throws IOException
{
    Queue popcorn = new LinkedList();
    BufferedReader in = null;
    int j = 0;
    try {
        File file2 = new File("Events.txt");
        in = new BufferedReader(new FileReader(file2));

        String str;
        String [][] process = new String[][];
        while ((str = in.readLine()) != null) {
            String[] arr = str.split(" ");
            for(int i=0 ; i<str.length() ; i++){
            process[j][i] = in.readLine();
        }
            j++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

} 

这不起作用。它抛出“变量必须提供维度表达式或数组 初始化器”

我正在尝试在此网页答案“http://www.chegg.com/homework-help/questions-and-answers/hired-biggy-s-popcorn-handle-popcorn-orders-store-write-java-console-application-reads-dat-q7220573”之后对其进行建模 我很确定这是行不通的。无论如何,这个链接列表似乎不起作用。就我的 String[][] 流程声明而言,有人可以指出我正确的方向吗?

最佳答案

您不能只初始化没有维度参数的数组。例如,这是无效的:

int[] array = new int[];

它需要像这样初始化:

int[] array = new int[10];

或者简单地说:

int[] array;
// ... //
array = new int[10];

多维数组也是如此。要创建一个包含 3 个大小为 5 的数组的数组,您可以输入:

int[][] array = new int[3][5];

但是,对于二维数组,您还可以输入:

int[][] array = new int[3][];
array[0] = new int[5];
array[1] = new int[7];
// ... //

重点是,您需要定义基本数组中还有多少个其他数组,并且还可以选择定义所有数组的大小或稍后添加它们。在这种情况下,您需要更改

String [][] process = new String[][];

类似于

String [][] process = new String[x][y];

关于java - 链接列表中的多维数组不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40815354/

相关文章:

c++ - 如何在不受其他文件影响的情况下在 Visual Studio 项目中运行 C++ 文件 "by itself"?

java - 爱欲 |只读文件系统错误

java - 复合模式实现

java - JAR 在其自己的安装目录中没有写入权限 (Windows 8)

java - Retrofit 2.0 多重拦截器

arrays - 将对象数组转换为集合

c - 将数组转换为指针时是否存在隐式 '&' 计算?

java - JPA 主键值始终为 0

Java从Object[]中获取特定元素

python - 使用 eval 优化 Python 读取大文件