簡體   English   中英

多個2D陣列

[英]Multiple 2D Arrays

假設我們有一個未指定尺寸的2D數組,我想創建一個將生成該數組的倍數的方法,這樣的方法:

tile(int [] []列表,int行,int列){...}

將給我該數組按int列水平擴展,並按int行垂直擴展。

你該怎么辦? 我的嘗試是:

public static int[][] tile(int[][] list, int row, int column) {
    int [][] renewed = new int[row*(list.length)][column*(list[0].length)];

        for (int i = 0; i<list.length; i++) {
        for (int j = 0; j<renewed.length; j+=row) {
           renewed[j] =list[i];
        }
    }

但是我在獲得想要的結果方面沒有任何運氣。 看來這種方式需要3〜4個嵌套的for循環來考慮每種可能性,並立即失控。 誰能給我一個線索?

嘗試這個:

int [][] list={{12,3},{4,6}};
    //loop through each array in the 2d array
    for(int x=0;x<list.length;x++){

            //make copy of array
            int[] listcopy=list[x].clone();
            //expand length of array by one
                    list[x]=new int [listcopy.length+1];
                    list[x][list.length-1]=0;
            for(int p=0;p<listcopy.length;p++){
                //copy each index to new array
                list[x][p]=listcopy[p];
            }

    }
    //make copy of 2d array
    int[][] listcopy=list.clone();
    //expand length of 2d array by 1
    list=new int[listcopy.length+1][];
    list[list.length-1]=new int[1];
    //assign values to the 2d array
    for(int x=0;x<listcopy.length;x++){
        list[x]=listcopy[x];
    }

它將新數組索引的值設置為0。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM