Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.Arrays; public class Main { /** * return a 2d array with the value and its count. Can deal with multiples. * Values should be in the same order as array (with subsequent duplicate * values removed). * * * @param array * the array * @return the count */ public static Double[][] getCount(double[] array) { return getCount(array, false); } /** * return a 2d array with the value and its count. Can deal with multiples. * Values will be in the same order as array (with subsequent duplicate * values removed). * * * @param array * the array * @param verbose * the verbose * @return the count */ public static Double[][] getCount(double[] array, boolean verbose) { ArrayList<Double[]> countArray = new ArrayList<Double[]>(); double count = 0; for (int i = 0; i < array.length; ++i) { // check if list contains current value boolean unique = true; for (Double[] ia : countArray) { if (ia[0].doubleValue() == array[i]) { unique = false; } } // Count values if (unique) { count = 1; for (int j = i + 1; j < array.length; ++j) { if (array[i] == array[j]) { count++; } } countArray.add(new Double[] { array[i], count }); } } Double[][] doubleArray = new Double[countArray.size()][]; doubleArray = countArray.toArray(doubleArray); if (verbose) { System.out.println("Value and count of value:\n" + Arrays.deepToString(doubleArray)); } return doubleArray; } /** * return a 2d array with the value and its count. Can deal with multiples. * Values will be in the same order as array (with subsequent duplicate * values removed). * * * @param array * the array * @param verbose * the verbose * @return the count */ public static String[][] getCount(String[] array, boolean verbose) { ArrayList<String[]> countArray = new ArrayList<String[]>(); double count = 0; for (int i = 0; i < array.length; ++i) { // check if list contains current value boolean unique = true; for (String[] ia : countArray) { if (ia[0].equals(array[i])) { unique = false; } } // Count values if (unique) { count = 1; for (int j = i + 1; j < array.length; ++j) { if (array[i] == array[j]) { count++; } } countArray.add(new String[] { array[i], String.valueOf(count) }); } } // Log.d(countArray); String[][] stringArray = new String[countArray.size()][]; stringArray = countArray.toArray(stringArray); if (verbose) { System.out.println("Value and count of value:\n" + Arrays.deepToString(stringArray)); } return stringArray; } }