【问题标题】:Resize primitive 2D array调整原始二维数组的大小
【发布时间】:2015-02-27 23:33:33
【问题描述】:

鉴于以下二维 (2D) 整数数组,我如何调整数组的大小,类似于 Python 的 Numpy's resize() function

matrix = [
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8, 9 ]
]

代码

public class MatrixUtils {
    public static void main(String[] args) {
        int[][] matrix = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

        System.out.println(formatMatrix(matrix));
        System.out.println(formatMatrix(resize(matrix, 4, 4)));
        System.out.println(formatMatrix(resize(matrix, 2, 2)));
    }

    public static int[][] resize(int[][] matrix, int w, int h) {
        return addRows(addCols(matrix, w-getWidth(matrix)), h-getHeight(matrix));
    }
}

使用System.arraycopy(),您可以通过在内存中保留一个新位置并更改指针来修改数组的长度。

System.arraycopy(matrix, 0, copy, 0, n > 0 ? oldHeight : newHeight);

预期输出

1,2,3
4,5,6
7,8,9

1,2,3,0
4,5,6,0
7,8,9,0
0,0,0,0

1,2
4,5

【问题讨论】:

    标签: java arrays dynamic resize primitive-types


    【解决方案1】:

    使用下面的代码,您可以更改数组的维度。还有几个格式化函数来配置分隔符。

    public class MatrixUtils {
           public static void main(String[] args) {
                  int[][] matrix = null;
    
                  matrix = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    
                  System.out.println(formatMatrix(matrix, "=", "-") + '\n');
                  System.out.println(formatMatrix(matrix) + '\n');
                  System.out.println(formatMatrix(resize(matrix, 4, 4)) + '\n');
                  System.out.println(formatMatrix(resize(matrix, 2, 2)) + '\n');
                  System.out.println(formatMatrix(resize(matrix, 10, 15)) + '\n');
           }
    
           public static final int getWidth(final int[][] matrix) {
                  int max = 0;
                  if (matrix.length > 0) {
                         max = matrix[0].length;
                         if (matrix.length > 1) {
                               for (int i = 1; i < matrix.length; i++) {
                                      if (matrix[i].length > max) {
                                             max = matrix[i].length;
                                      }
                               }
                         }
                  }
                  return max;
           }
    
           public static final int getHeight(final int[][] matrix) {
                  return matrix.length;
           }
    
           public static int[][] resize(final int[][] matrix, final int w, final int h) {
                  int width = getWidth(matrix);
                  int height = getHeight(matrix);
                  return addRows(addCols(matrix, w - width), h - height);
           }
    
           private static int[][] addRows(final int[][] matrix, final int n) {
                  if (n == 0) {
                         return matrix;
                  }
                  int oldHeight = matrix.length;
                  int newHeight = oldHeight + n;
                  int width = getWidth(matrix);
                  int[][] copy = new int[newHeight][];
                  System.arraycopy(matrix, 0, copy, 0, n > 0 ? oldHeight : newHeight);
                  for (int i = oldHeight; i < newHeight; i++) {
                         copy[i] = new int[width];
                  }
                  return copy;
           }
    
           private static int[][] addCols(final int[][] matrix, final int n) {
                  if (n == 0) {
                         return matrix;
                  }
                  int oldWidth = getWidth(matrix);
                  int newWidth = oldWidth + n;
                  int height = matrix.length;
                  int[][] copy = new int[height][newWidth];
                  for (int i = 0; i < height; i++) {
                         copy[i] = new int[newWidth];
                         System.arraycopy(matrix[i], 0, copy[i], 0, n > 0 ? oldWidth
                                      : newWidth);
                  }
                  return copy;
           }
    
           public static String formatMatrix(final int[][] matrix) {
                  return formatMatrix(matrix, "\n", ",").toString();
           }
    
    
           public static String formatMatrix(final int[][] matrix, String vSep, String hSep) {
                  return join(new StringBuffer(), matrix, vSep, hSep).toString();
           }
    
           public static StringBuffer join(final int[][] arr, String vSep, String hSep) {
                  return join(new StringBuffer(), arr, vSep, hSep);
           }
    
           public static StringBuffer join(final int[] arr, String sep) {
                  return join(new StringBuffer(), arr, sep);
           }
    
           protected static StringBuffer join(StringBuffer buff, final int[][] arr, String vSep, String hSep) {
                  if (arr.length > 0) {
                         join(buff, arr[0], hSep);
                         for (int i = 1; i < arr.length; i++) {
                               join(buff.append(vSep), arr[i], hSep);
                         }
                  }
                  return buff;
           }
    
           protected static StringBuffer join(StringBuffer buff, int[] arr, String sep) {
                  if (arr.length > 0) {
                         buff.append(arr[0]);
                         for (int i = 1; i < arr.length; i++) {
                               buff.append(sep).append(arr[i]);
                         }
                  }
                  return buff;
           }
    }
    

    输出

    1-2-3=4-5-6=7-8-9
    
    1,2,3
    4,5,6
    7,8,9
    
    1,2,3,0
    4,5,6,0
    7,8,9,0
    0,0,0,0
    
    1,2
    4,5
    
    1,2,3,0,0,0,0,0,0,0
    4,5,6,0,0,0,0,0,0,0
    7,8,9,0,0,0,0,0,0,0
    0,0,0,0,0,0,0,0,0,0
    0,0,0,0,0,0,0,0,0,0
    0,0,0,0,0,0,0,0,0,0
    0,0,0,0,0,0,0,0,0,0
    0,0,0,0,0,0,0,0,0,0
    0,0,0,0,0,0,0,0,0,0
    0,0,0,0,0,0,0,0,0,0
    0,0,0,0,0,0,0,0,0,0
    0,0,0,0,0,0,0,0,0,0
    0,0,0,0,0,0,0,0,0,0
    0,0,0,0,0,0,0,0,0,0
    0,0,0,0,0,0,0,0,0,0
    

    【讨论】:

    • 你为什么要回答自己的问题?!​​
    • @Mr.Polywhirl 你想要神圣的燃料。我的任务到此结束。
    • @Mr.Polywhirl 够公平的。我也分享了我的知识。
    【解决方案2】:
    static int[][] resize(int[][] matrix, int w, int h) {
        int[][] temp = new int[h][w];
        h = Math.min(h, matrix.length);
        w = Math.min(w, matrix[0].length);
        for (int i = 0; i < h; i++)
            System.arraycopy(matrix[i], 0, temp[i], 0, w);
        return temp;
    }
    

    【讨论】:

    • 你还是得解释一下:)
    猜你喜欢
    • 2011-02-08
    • 2017-01-05
    • 2018-05-02
    • 1970-01-01
    • 2020-11-15
    • 2016-02-22
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    相关资源
    最近更新 更多