填充动态2D java数组

填充动态2D java数组,java,arrays,dynamic,linked-list,Java,Arrays,Dynamic,Linked List,我想读入一个文件,并将每一行的内容写入一个维度数组[三个][不确定,但相当长] 到目前为止,我有下面的代码,它能够使用模式匹配器梳理出我正在寻找的输入文件的组件,但是,这被困在输入的第一行,只是一遍又一遍地添加,如何使输入文件前进并每次向数组写入新行 到目前为止,我的代码如下所示: public static void main(String[] args) throws IOException { BufferedReader br_0 = new BufferedReader

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

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

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

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

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

[0][2]
香港

[1] [0]

[1] [1]
午夜钟声

[1] [2]

[2] [0]
参与

[3] [1]
仪式

[2] [2]
两国

阵列长度必须能够容纳很长的文件,但也能容纳很短的文件,这一点很重要

此时的输出如下所示:

'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 # , #').
...
[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++) 
但是,不应使用幻数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();
List arr=new ArrayList();
模式p=Pattern.compile(“(.*?”(?![a-zA-Z])”;
//当文件仍在读取时
while((line_0=br_0.readLine())!=null){
列表三=新的ArrayList();
匹配器m=p.Matcher(第0行);
int j=0;
while(m.find()){
三、增加(m组(1));
}
arr.add(三个);
}
br_0.close();
要打印

for( List<String> three: arr ){
    for( String s: three ){
        System.out.print( s  + " " );
    }
    System.out.println();
}
for(列表三:arr){
用于(字符串s:3){
系统输出打印(s+“”);
}
System.out.println();
}

这将处理第一行262978次

for (int count = 0; count < 262978; count++) 
但是,不应使用幻数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();
List arr=new ArrayList();
模式p=Pattern.compile(“(.*?”(?![a-zA-Z])”;
//当文件仍在读取时
while((line_0=br_0.readLine())!=null){
列表三=新的ArrayList();
匹配器m=p.Matcher(第0行);
int j=0;
while(m.find()){
三、增加(m组(1));
}
arr.add(三个);
}
br_0.close();
要打印

for( List<String> three: arr ){
    for( String s: three ){
        System.out.print( s  + " " );
    }
    System.out.println();
}
for(列表三:arr){
用于(字符串s:3){
系统输出打印(s+“”);
}
System.out.println();
}

您正在从此处的文件中读取数据:
而((line_0=br_0.readLine())!=null)
,但是,您正在使用该行执行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++;    
}
inti=0;
模式p=Pattern.compile(“(.*?”(?![a-zA-Z])”;
//从零开始计数
字符串[][]arr=新字符串[262978][3];
而((line_0=br_0.readLine())!=null)和&(i<262978))
{
匹配器m=p.Matcher(第0行);
int j=0;
while(m.find())
{
arr[i][j++]=m组(1);
}
i++;
}

您正在从此处的文件中读取数据:
而((line_0=br_0.readLine())!=null)
,但是,您正在使用该行执行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++;    
}
inti=0;
模式p=Pattern.compile(“(.*?”(?![a-zA-Z])”;
//从零开始计数
字符串[][]arr=新字符串[262978][3];
而((line_0=br_0.readLine())!=null)和&(i<262978))
{
匹配器m=p.Matcher(第0行);
int j=0;
while(m.find())
{
arr[i][j++]=m组(1);
}
i++;
}

请添加您的实际输出(或创建一个:-)请添加您的实际输出(或创建一个:-)),更好的方法是使用自定义类替换字符串数组。只需迭代列表-请参阅扩展答案。这将导致一个错误:java.lang.ArrayIndexOutOfBoundsException位于第三行[j++]=m.group(1);那么你认为这些行总是产生3个或更少的字符串的假设是不正确的。-修改了代码-看看它做了什么。更好的是用自定义类替换字符串数组。只需迭代列表-看看扩展答案。这会导致一个错误:java.lang.ArrayIndexOutOfBoundsException在第三行[j++]=m.group(1);那么你认为这些行总是产生3个或更少的字符串的假设是不正确的。-修改代码-查看它的功能。