Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将二维矩阵转换为列的字符串数组(java)_Java - Fatal编程技术网

将二维矩阵转换为列的字符串数组(java)

将二维矩阵转换为列的字符串数组(java),java,Java,我对java中的ArrayList知之甚少。我需要解决以下问题:如何生成二维矩阵列的数组[]。输入文件是: vgvvgvv efvvf E V E V V V V A V V A G V A D vdvdevv V V V V V Stringarrayliner(见下文)所需的格式为: {“VEVAVA”、“GFVADV”、“VVVV”、“VVVVV”、“GEVGEV”、“VVVV”、“VEVAVA”、“VVVVV”} 我的密码是: ArrayList mat=new ArrayList();

我对java中的ArrayList知之甚少。我需要解决以下问题:如何生成二维矩阵列的数组[]。输入文件是:

vgvvgvv
efvvf E V E V
V V V
A V V A G V A D
vdvdevv
V V V V V

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

{“VEVAVA”、“GFVADV”、“VVVV”、“VVVVV”、“GEVGEV”、“VVVV”、“VEVAVA”、“VVVVV”}

我的密码是:

ArrayList mat=new ArrayList();
扫描仪扫描=新扫描仪(新文件(“internal2”));
字符串[]线性=新字符串[m];
while(scan.hasNextLine()){
Scanner colReader2=新扫描仪(scan.nextLine());
while(colReader2.hasNext())
{
对于(int i=0;i
Scanner.next()
只扫描字符直到下一个空格。如果要扫描整行,请使用
Scanner.nextLine()
,它应该为您提供
“vgvvgvv”
。要删除字符之间的空格,请使用
String.replaceAll(“\s”,”)

更新:对不起,我误解了你的问题

要获得所需的输出,请将
liner[i]=colReader2.next()
更改为
liner[i]+=colReader2.next()

它应该向数组项追加一个字符,而不是替换它

更新#2:我测试了您的代码片段,发现了一些问题

  • liner[i]
    未使用空字符串初始化,从而导致输出
    nullVEVAVA
    等等…我没有检查,抱歉
  • 你不需要使用两个扫描仪,一个就足够了
  • mat.add(liner)
    仅将引用添加到数组中,因此如果更改该数组,这些更改也将在ArrayList中可见
  • 通常,最好先解析输入文件,然后创建一个数组,因为您不必知道维度
  • 假设您想要一个字符串数组的ArrayList,其中包含(我假设它不是锯齿状的)输入的列,这应该可以做到(好吧,至少它给了我所需的输出):

    ArrayList input=new ArrayList();
    Scanner sc=新扫描仪(新文件(“输入”);
    //读取输入
    while(sc.hasNextLine()){
    input.add(sc.nextLine().split(\\s));
    }
    sc.close();
    //这只有在你的输入不是锯齿状的情况下才有效!
    String[][]mat=新字符串[input.get(0.length]];
    //将矩阵从[line][column]转换为[column][line]
    对于(int i=0;i

如果这仍然没有帮助,我很抱歉,但我的时间和你的一样宝贵,我相信你可以通过做一些像样的研究来解决许多问题。但是,如果你真的陷入困境,请回来问另一个问题,我们会乐意帮你解决;)

在数组列表中使用StringBuilder并附加到它。我猜在您开始接受值之前,矩阵的大小是已知的?不确定您希望从

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();
List mat=new ArrayList();//最好声明为List,数组List是1个实现
扫描仪扫描=新扫描仪(新文件(“internal2”));
int col=0;
while(scan.hasNextLine()){
col=0;
Scanner colReader2=新扫描仪(scan.nextLine());
while(colReader2.hasNext())
{
如果(材料尺寸()<(柱+1)){
材料添加(新的StringBuilder());
}
//mat.get(col).substring(mat.get(col).length()-1);
mat.get(col.append)(colReader2.next())
}
}
//scan.nextLine();

}

谢谢你的回答。我编辑了我的问题,所以请检查并进一步帮助我。你的
行文是什么样子的?请同时发布你的实际输出,而不仅仅是你想要的。和(对于你的下一个问题)在你的工作完成之前不要改变你的问题,如果你再次卡住了,就打开一个新的问题
R
R
Q
R
R
Q
R
R
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");
    }
}
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();