java - 填充动态二维java数组

标签 java arrays dynamic linked-list

我想读入一个文件,并将每行的内容写入一个维度为[三][不确定,但很长]的数组

到目前为止,我有以下代码,它能够使用模式匹配器梳理出我正在寻找的输入文件的组件,但是,它卡在输入的第一行,只是添加了该内容一遍又一遍,如何使输入文件进展并每次向数组写入新行。

到目前为止,我的代码如下所示:

public static void main(String[] args) throws IOException
{   

    BufferedReader br_0 = new BufferedReader(new FileReader("file.txt"));
    String line_0;

    //while the file is still reading
    while ((line_0 = br_0.readLine()) != null) 
    {           

        int i = 0;
        Pattern p = Pattern.compile("'(.*?)'(?![a-zA-Z])");
        //count from zero
        String[][] arr = new String[262978][3];

        for (int count = 0; count < 262978; count++) 
        {

            Matcher m = p.matcher(line_0);

            int j = 0;
            while (m.find()) 
            {
                arr[i][j++] = m.group(1);
            }
            i++;

        }
    }
    br_0.close();
}

输入文件如下所示:

'end with'('the playing of the british national anthem', 'hong kong').
'follow at'('the stroke of midnight', 'this').
'take part in'('the ceremony', 'both countries').
'start at about'('# pm', 'the ceremony').
'end about'('# am', 'the ceremony').
'lower'('the british hong kong flag', '# royal hong kong police officers').
'raise'('the sar flag', 'another #').
'leave for'('the royal yacht britannia', 'the #').
'hold by'('the chinese and british governments', 'the handover of hong kong').
'rise over'('this land', 'the regional flag of the hong kong special administrative region of the people \'s republic of china').
'cast eye on'('hong kong', 'the world').
'hold on'('schedule', 'the # governments').
'be festival for'('the chinese nation', 'this').
'go in'('the annals of history', 'july # , #').
...

理想情况下,数组索引如下所示:

[0][0]结尾为

[0][1]演奏英国国歌

[0][2]香港

[1][0]关注

[1][1]午夜钟声

[1][2]这个

[2][0]参加

[3][1]仪式

[2][2]两国

重要的是数组长度既能容纳很长的文件,又能容纳很短的文件。

此时的输出如下所示:

[45993][2] the president of the people \'s republic of china he mr jiang zemin
[45994][0] speak at
[45994][1] the ceremony
[45994][2] the president of the people \'s republic of china he mr jiang zemin
[45995][0] speak at
[45995][1] the ceremony
[45995][2] the president of the people \'s republic of china he mr jiang zemin
[45996][0] speak at
[45996][1] the ceremony
[45996][2] the president of the people \'s republic of china he mr jiang zemin
[45997][0] speak at
[45997][1] the ceremony
[45997][2] the president of the people \'s republic of china he mr jiang zemin
[45998][0] speak at
[45998][1] the ceremony
[45998][2] the president of the people \'s republic of china he mr jiang zemin
[45999][0] speak at

最佳答案

这会处理第一行 262978 次。

for (int count = 0; count < 262978; count++) 

更好的是:

int count = 0;
String[][] arr = new String[262978][3];
Pattern p = Pattern.compile("'(.*?)'(?![a-zA-Z])"); 
//while the file is still reading
while ((line_0 = br_0.readLine()) != null) {           
     Matcher m = p.matcher(line_0);
     int j = 0;
     while (m.find()) {
         arr[count][j++] = m.group(1);
     }
     count++;
}

br_0.close();

但是,不应使用魔数(Magic Number) 262978,也不应使用数组。显然也是最大的假设。每行三个字符串不正确。

将其替换为

List<List<String>> arr = new ArrayList<>();
Pattern p = Pattern.compile("'(.*?)'(?![a-zA-Z])"); 
//while the file is still reading
while ((line_0 = br_0.readLine()) != null) {
     List<String> three = new ArrayList<>();         
     Matcher m = p.matcher(line_0);
     int j = 0;
     while (m.find()) {
         three.add( m.group(1) );
     }
     arr.add( three );
}

br_0.close();

打印,

for( List<String> three: arr ){
    for( String s: three ){
        System.out.print( s  + " " );
    }
    System.out.println();
}

关于java - 填充动态二维java数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28106700/

相关文章:

java - java 8中有 "instanceof "的替换吗?

java - 在信封上绘制文字

javascript - JSON.parse 给我对象而不是结果

java - 如何让我的程序接受任意数量的数组而不是一组?

css - 如果没有 js 太小则隐藏元素

java - 使用 Matlab Builder JA 时遇到的问题

java - 强制子类重写这两个方法或一个都不重写

javascript - 如何将嵌套对象转换为javascript中的数组?

c# - 动态创建类型

c# - 在 Silverlight 应用程序中使用 "dynamic"关键字时出错