2

Here is what I am trying to do, say I have an input file (input.txt) in this format, the number of rows or columns can be different, columns are separated by spaces:

the       DT   B-NP
current   JJ   I-NP
account   NN   I-NP
deficit   NN   I-NP
will      MD   B-VP << CURRENT TOKEN
narrow    VB   I-VP
to        TO   B-PP
only      RB   B-NP

I want to get each word into an element of a 2 dimension array x[i,j] so that, I can use an index file:

x[0,0]
x[0,1]
x[-1,0]
x[-2,1]

to get this result:

will
MD
deficit
NN

With the number in the bracket is the i,j index of the array, the starting position [0,0] is the first word of the line that marked by "<< CURRENT TOKEN" (in this case is word "will" ).

So now I can read the file to array by:

import java.awt.List;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;



public class ReadfileIntoArray {

    String[] data = new String[100];

    public void read() throws IOException {


        FileReader fr = new FileReader("/Users/home/Documents/input.txt");
        BufferedReader br = new BufferedReader(fr);
        String line;

        System.out.println("Print from here:");
        int i = 0;
        while ((line = br.readLine()) != null) {
            data[i] = line;
            System.out.println(data[i]);
            i++;
        }
        br.close();
        // This is for resize the data array (and data.length reflect new size)
        String[] dataNew = new String[i];
        System.arraycopy(data, 0, dataNew, 0, i);
        data = dataNew;
        System.out.println("Data length: " + data.length);
    }

    public static void main(String[] args) throws IOException {
        ReadfileIntoArray rfta = new ReadfileIntoArray();
        rfta.read();
    }
}

But as I searched, I might need to use

List<String> arrList =FileUtils.readLines(new File("myfile.txt"));  

for undefined length (??) not very sure but I think need to import special package to use it, I got error _ Second thing is how to determined the starting element at position [0,0], and how to indicate negative index like [-2,1]...

How can I managed to do the task above, it look quite complicated for me. Thanks alot !

4

1 回答 1

0

您可以将 ArrayList 的 ArrayList 用于不定长度。

public void read() throws IOException {
    List<ArrayList<String>> mylist = new ArrayList<ArrayList<String>>();
    FileReader fr = new FileReader("/Users/home/Documents/input.txt");
    BufferedReader br = new BufferedReader(fr);
    String line;
    int current_line = 0; 
    int cnt = 0;

    while ((line = br.readLine()) != null) {
        String arr[];
        arr = line.split("\t");
        mylist.add(new ArrayList<String>());
        for(int i = 0; i < arr.length; i++){
            mylist.get(cnt).add(arr[i]);
            if(mylist.get(cnt).get(i).equals("<<")) 
                current_line = cnt;
        }
        cnt++;
    }
    br.close();
}

现在你有了arraylist的arraylist。

要获取第 i_th 行的第 j_th 个元素,您应该编写mylist.get(i).get(j)

这样,您将不需要增加数组的大小,并且行的长度也无关紧要,即第一行是 10 个字符串,第二行是 15 个字符串是可以接受的。

此外,不要将负索引保留current_line为变量,对于上一行,请current_line - 1用作索引。

于 2013-05-28T11:05:48.807 回答