Java Collection How to - Find median of 2D arrays








Question

We would like to know how to find median of 2D arrays.

Answer

import java.util.Arrays;
//  w  w  w  . j  a  va 2s. c o m
public class Main {

  public static void main(String[] args) {
    int[][] array2d = { { 21, 14, 13, 12, 15 }, { 4, 25, 23, 22, 9 },
        { 4, 7, 8, 98, 24 } };
    int[] list = new int[array2d.length * array2d[0].length];

    int listPos = 0;
    for (int i = 0; i < array2d.length; i++) {
      for (int j = 0; j < array2d.length; j++) {
        list[listPos++] = array2d[i][j];
      }
    }
    Arrays.sort(list);
    System.out.println(median(list));
  }

  public static double median(int[] m) {
    int middle = m.length / 2;
    if (m.length % 2 == 1) {
      return m[middle];
    } else {
      return (m[middle - 1] + m[middle]) / 2.0;
    }
  }
}

The code above generates the following result.