简体   繁体   中英

populate dynamic 2D java array

I want to read in a file, and write the contents of each line to an array of dimensions [three][indeterminate, but quite long]

Thus far I have the following code, which is able to tease out, using pattern matcher, the components of the input file that I'm looking for, however, this get's stuck on the first line of input and just adds that over and over, how to make the input file progress and write a new line to the array each time.

Thus far my code looks like this:

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

The input file looks like this:

'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 # , #').
...

Ideally the array indices would look like this:

[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

The output at this point look like this:

[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

This processes the first line 262978 times.

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

Better is:

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

However, the magic number 262978 should not be used, and neither an array. Apparently also the assumption of max. three strings per line is not correct.

Replace this by

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

To print,

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

You are reading data from the file in here: while ((line_0 = br_0.readLine()) != null) , however, you are doing 262978 iterations with that same line here: for (int count = 0; count < 262978; count++) .

What you could do, would be to replace it with something like so:

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++;    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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