简体   繁体   English

如何打印不同索引长度的2D数组?

[英]How to print a 2D array of different index lengths?

I currently have a 2D array which looks the equivalent of: 我目前有一个2D数组,其外观等效于:

int[] my2Darray = {{1, 2}, {3, 4, 5}, {1}};
//the second dimensions can and do vary

I was wondering how I can print all elements of this array automatically. 我想知道如何自动打印此数组的所有元素。

My code to print currently looks like: 我当前要打印的代码如下:

for(int t = 0; t < movieactorsbulk.size(); t++) {
       temparray = movieactorsbulk.get(t).split("\\s");
       movieactorsindiv[t] = new String[temparray.length];
        for(int v = 0; v < temparray.length; v++) {
            movieactorsindiv[t][v] = temparray[v];
        }
    }

movieactorsbulk contains: [a00011974 a00011975, a00011975 a00011974, a77777777] movieactorsbulk包含:[a00011974 a00011975,a00011975 a00011974,a77777777]

So I am trying to separate the indexes of the ArrayList movieactorsbulk) and put it into a 2D array (movieactorsindiv) then print everything no matter the size. 因此,我试图将ArrayList movieactorsbulk的索引分开,然后将其放入2D数组(movieactorsindiv),然后不管大小都打印所有内容。

Right now I know my problem is that on the last go around the code splits "a77777777" and puts it into movieactorsindiv[2][0] but when I try to print based on temparray.length it only prints the first indexes as temparray[] only contains "a77777777" at that point. 现在,我知道我的问题是在最后一遍,代码将“ a77777777”拆分并放入movieactorsindiv [2] [0],但是当我尝试基于temparray.length进行打印时,它仅将第一个索引打印为temparray [ ]此时仅包含“ a77777777”。 How can I print all indexes of movieactorsindiv ([0][0] through to [x][y]; where x and y can be any number)? 如何打印movieactorsindiv的所有索引(从[0] [0]到[x] [y];其中x和y可以是任何数字)?

Any help would be appreciated. 任何帮助,将不胜感激。 Sorry if the question isn't easy to understand. 很抱歉,这个问题不容易理解。 :S :S

Try using this skeleton of a traversal: 尝试使用遍历的以下骨架:

for (int i = 0; i < matrix.length; i++) {
    for (int j = 0; j < matrix[i].length; j++) {
        // print matrix[i][j]
    }
}

It takes into consideration the case where each row's columns can be of different length. 它考虑了每一行的列可以具有不同长度的情况。 It works for 2-D matrices of any height and width (even of varying widths), and it'll be easy to adapt for your problem in particular. 它适用于任何高度和宽度(甚至宽度不同)的二维矩阵,并且特别容易适应您的问题。

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

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