1

我对java中的ArrayLists了解不多。我需要解决以下问题:如何制作二维矩阵列的数组[]。输入文件是:

VGV VV GVVV
EFV VF EVEV
VVV VV VVVV
AAV VA GVAD
VDV VD EVVV
AVV VV VVAV

Stringarray 所需的格式liner(见下文)是:

{"VEVAVA","GFVADV","VVVVVV","VVVFVVVAVDVV","GEVGEV","VVVVVV","VEVAVA","VVVDVV"}

我的代码是:

    ArrayList<String[]> mat = new ArrayList<String[]>();


Scanner scan = new Scanner(new File("internal2"));
String[] liner = new String[m];
while (scan.hasNextLine()) {

    Scanner colReader2 = new Scanner(scan.nextLine());
    while(colReader2.hasNext())
    {
        for(int i = 0; i<m; i++) {
            liner[i] = colReader2.next();
//System.out.println(liner[i]);
            mat.add(liner);
            }

    }
//        scan.nextLine();
}

这样做的目的是我想在liner. 现在程序似乎只给出liner这样的: {"A","V","V","VV","V","V","A","V"} 这是最后一行输入文件。我希望你能帮助我。

编辑

我的代码继续:

Pattern pattern = Pattern.compile("[A-G]+");
Pattern pattern2 = Pattern.compile("[V]+");

String[][] matrix4 = mat.toArray(new String[n][m]);

for (int i = 0; i < m; i++) {

    StringBuffer sf = new StringBuffer();
    for (int j = 0; j < n; j++) {
        sf.append(matrix4[j][i]);

    }

    Matcher matcher = pattern.matcher(sf.toString());
    Matcher matcher2 = pattern2.matcher(sf.toString());

    if (matcher.find()) {
        System.out.println("R");
    } else if (matcher2.matches()) {
        System.out.println("Q");
    }

}

因此,对于liner包含至少 1次A-G出现的列字符串,R必须打印。对于仅包含V's 的列字符串,它必须打印Q。那么输出应该是:

R
R
Q
R
R
Q
R
R

但这不是我得到的。你们有谁知道我做错了什么?

解决了:

我不得不null通过使用 s 从衬垫中取出Arrays.fill(liner, "");

4

2 回答 2

0

Scanner.next() only scans characters until the next whitespace. If you want to scan a whole line use Scanner.nextLine(), which should give you "V G V VV G V V V". To remove the whitespace between the characters use String.replaceAll("\s", "") on that.

Update: sorry, I misunderstood your question.

To get your desired output, change liner[i] = colReader2.next() to liner[i] += colReader2.next()

That should append a character to the array entry instead of replacing it.

Update #2: I tested your code snippets and found some problems.

  • liner[i] isn't initialized with an empty String which leads to the output nullVEVAVA and so on ... I didn't check for that, sorry.
  • you don't need to use two Scanners, one is enough.
  • mat.add(liner) only adds the reference to your array, so if you change that array, these changes will also be visible in the ArrayList
  • in general, it would be better to first parse your input file and then create an array, because you don't have to know the dimensions
  • assuming you want an ArrayList of an Array of Strings holding the columns of the (I assume that it's not jagged) input, this should do it (well, at least it gives me the desired output):

    ArrayList<String[]> input = new ArrayList<String[]>();
    
    Scanner sc = new Scanner(new File("input"));
    
    // read the input
    while (sc.hasNextLine()) {
        input.add(sc.nextLine().split("\\s"));
    }
    
    sc.close();
    
    // this only works if your input isn't jagged!
    String[][] mat = new String[input.get(0).length][];
    
    // transform your matrix from [line][column] to [column][line]
    for (int i = 0; i < input.size(); i++) {
        for (int j = 0; j < input.get(i).length; j++) {
            if (mat[j] == null) {
                mat[j] = new String[input.size()];
            }
            if (mat[j][i] == null) {
                mat[j][i] = "";
            }
            mat[j][i] += input.get(i)[j];
        }
    }
    
    Pattern pattern = Pattern.compile("[A-G]+");
    Pattern pattern2 = Pattern.compile("[V]+");
    
    for (int i = 0; i < mat.length; i++) {
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < mat[i].length; j++) {
            sb.append(mat[i][j]);
        }
    
        Matcher matcher = pattern.matcher(sb.toString());
        Matcher matcher2 = pattern2.matcher(sb.toString());
    
        if (matcher.find()) {
            System.out.println("R");
        } else if (matcher2.matches()) {
            System.out.println("Q");
        }
    }
    

If this still doesn't help, I'm sorry, but my time is as precious as yours and I'm sure you can solve many problems by doing some decent research. However, if you get really stuck, just come back and ask another question and we will willingly help you out ;)

于 2013-03-10T20:39:29.723 回答
0

Use a StringBuilder in the array list and append to it. I guess the size of the matrix is known before you start accepting values? Not sure what you are expecting from the

List<StringBuilder[]> mat = new ArrayList<StringBuilder[]>();//better to declare as list, array list is 1 implementation


Scanner scan = new Scanner(new File("internal2"));
int col = 0;    
while (scan.hasNextLine()) {
    col = 0;
    Scanner colReader2 = new Scanner(scan.nextLine());
    while(colReader2.hasNext())
    {

        if(mat.size() < (col + 1)){
            mat.add(new StringBuilder());
        }
        //mat.get(col).substring(mat.get(col).length() - 1);
        mat.get(col).append(colReader2.next())
        }

}
//        scan.nextLine();

}

于 2013-03-10T20:48:58.543 回答