简体   繁体   English

输出二维arrayList的内容

[英]Outputting contents of a 2d arrayList

Im strugging to get this code to work. 我正在努力使此代码正常工作。 What is supposed to happen is it creates an 2d arraylist, then adds some arraylist to that 2darraylist. 应该发生的事情是创建一个2d arraylist,然后向该2darraylist添加一些arraylist。 Then taking user input and adding it to 2d array and tokenise it. 然后接受用户输入并将其添加到2d数组中并对其进行标记化。 Then output those tokenised inputs. 然后输出那些标记化的输入。 The output should be so the first line will show the first arrayList then the second line will show the contents of the second arraylist and so on. 输出应该是这样,第一行将显示第一个arrayList,然后第二行将显示第二个arraylist的内容,依此类推。 Im getting the error message outofbound, size 5 size 5. 我收到错误消息出站,大小为5,大小为5。

    String transState;
    trans = new ArrayList<List<String>>(5);
    ArrayList<String> t = new ArrayList<String>(5);
    for (i = 1; i < 5; i++) {
    trans.add(t);
    }
    for (i = 1; i < 5; i++) {

        for (j = 0; j < 5; j++){
            transState = s.nextLine();
            trans.get(i).get(j).add(transState);
            trans.get(i).get(j).split(transState);
        }
    }
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 5; j++) {  
            System.out.println( trans.get(i).get(j) );
        }
    }

Seems like the problem is here: 似乎问题出在这里:

ArrayList<String> t = new ArrayList<String>(5);
for (j = 0; j < 5; j++)
    trans.add(t);

You're adding the same ArrayList several times when you should add different ArrayList s into the principal ArrayList , like this: 你添加相同ArrayList几次时,你应该添加不同ArrayList s转换的主要ArrayList ,像这样的:

for (j = 0; j < 5; j++) {
    ArrayList<String> t = new ArrayList<String>(5);
    trans.add(t);
}

Apart of this, you will have a problem here: 除此之外,您将在这里遇到问题:

trans.get(i).get(j).add(transState);

It should be 它应该是

trans.get(i).add(transState);
//trans.get(i) returns List<String>

Also, this line makes no sense as is: 此外,此行本身没有任何意义:

trans.get(i).get(j).split(transState);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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