繁体   English   中英

填充动态2D Java数组

[英]populate dynamic 2D java array

我想读取一个文件,并将每行的内容写到一个尺寸为[3] [不确定,但很长]的数组中

到目前为止,我有以下代码,可以使用模式匹配器来找出我要查找的输入文件的组件,但是,该代码停留在输入的第一行,并且一遍又一遍地添加,如何使输入文件进行进度并每次都在数组中写入新行。

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

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] end with

[0] [1] the playing of the british national anthem

[0] [2] hong kong

[1] [0] follow at

[1] [1] the stroke of midnight

[1] [2] this

[2] [0] take part in

[3] [1] the ceremony

[2] [2] both countries

重要的是数组长度能够容纳很长的文件,但也可以容纳短的文件。

此时的输出如下所示:

[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();

但是,不应使用幻数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();
}

您正在此处从文件中读取数据: while ((line_0 = br_0.readLine()) != null) ,但是,您正在此处for (int count = 0; count < 262978; count++) 同一行的262978迭代: for (int count = 0; count < 262978; count++)

您可以做的是将其替换为以下内容:

int i = 0;
Pattern p = Pattern.compile("'(.*?)'(?![a-zA-Z])");
//count from zero
String[][] arr = new String[262978][3];
while (((line_0 = br_0.readLine()) != null) && (i < 262978))
{
    Matcher m = p.matcher(line_0);

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

暂无
暂无

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

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