Jan Slominski
Jan Slominski

Reputation: 2978

Efficient way to trim 2d array in Java

I have a following 2 dimensional array:

int[][] array = new int[][]{
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
        {0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
        {0, 0, 1, 1, 0, 1, 1, 0, 0, 0},
        {0, 0, 1, 1, 0, 1, 0, 0, 0, 0},
        {0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
        {0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
        {0, 0, 0, 0, 0, 1, 0, 0, 0, 0}
};

and I would like to trim all the surrounding zeroes, so my output would be like this (removing "zeros" outside and preserving the zeroes that are surrounded by "ones"):

        {0, 1, 1, 1, 0},
        {0, 1, 1, 1, 1},
        {1, 1, 0, 1, 1},
        {1, 1, 0, 1, 0},
        {0, 1, 1, 1, 1},
        {0, 1, 1, 1, 1},
        {0, 0, 0, 1, 0}

I'm looking for an efficient way of doing this.

Upvotes: 1

Views: 1328

Answers (1)

Oneiros
Oneiros

Reputation: 4378

Possible solution (dunno if it is the most efficient way):

public static int[][] trim(int[][] mtx, int rmin, int rmax, int cmin, int cmax) {
   int[][] result = new int[rmax-rmin+1][];
   for (int r = rmin, i = 0; r <= rmax; r++, i++) {
      result[i] = Arrays.copyOfRange(mtx[r], cmin, cmax+1);
   }
   return result;
}

public static int[][] trim(int[][] mtx, int trimmed) {
   int cmin = mtx[0].length;
   int rmin = mtx.length;
   int cmax = -1;
   int rmax = -1;

   for (int r = 0; r < mtx.length; r++)
      for (int c = 0; c < mtx[0].length; c++)
         if (mtx[r][c] != trimmed) {
            if (cmin > c) cmin = c;
            if (cmax < c) cmax = c;
            if (rmin > r) rmin = r;
            if (rmax < r) rmax = r;
         }

   return trim(mtx, rmin, rmax, cmin, cmax);
}

public static void main (String[] args) {
   int[][] array = new int[][]{
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
      {0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
      {0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
      {0, 0, 1, 1, 0, 1, 1, 0, 0, 0},
      {0, 0, 1, 1, 0, 1, 0, 0, 0, 0},
      {0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
      {0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
      {0, 0, 0, 0, 0, 1, 0, 0, 0, 0}
   };
   int[][] trim = trim(array, 0);
   System.out.println(Arrays.deepToString(trim));
}

Upvotes: 3

Related Questions