Java 2D阵列逻辑实现

时间:2017-10-27 13:28:04

标签: java arrays sorting multidimensional-array

我今天得到了一个问题并尝试过,但没有找到相关的解决方案。 问题是:

假设我们有一个包含城市名称和产品总数的二维数组。城市名称可以在列表中重复。

现在我们要做的是

  1. 计算在一个城市销售的产品数量,并将其保存在另一个具有唯一城市名称和销售产品总数的二维阵列中。
  2. 打印销量最高的第二个城市。
  3. 可以考虑的样本列表是:

    private static String[][] arr = new String[][]{
            {"New Delhi", "5000"},
            {"Chennai", "4300"},
            {"Goa", "2940"},
            {"New Delhi", "2003"},
            {"Kolkata", "8904"},
            {"Kerala", "8972"},
            {"New Delhi", "8922"},
            {"Chennai", "8217"},
            {"New Delhi", "2462"},
            {"Kolkata", "5564"},
            {"Kerala", "9934"},
            {"New Delhi", "100"},
            {"Kolkata", "892"},
            {"Kerala", "9406"},
            {"New Delhi", "2003"},
            {"Chennai", "1049"}
        };
    

    注意:我已经使用Map Interface尝试了这个问题。仅寻找具有多维数组的解决方案

2 个答案:

答案 0 :(得分:4)

使用HashMap,其中key为city,value为总计数

  1. 迭代数组
  2. 如果key存在,请获取key的值并使用new增加计数 值
  3. 如果密钥不存在,请按下该值。
  4. 这是基本的想法:

    if(dataMap.get(city)==null){
      dataMap.put(city,value);
     }
    else{
     dataMap.put(city,value+dataMap.get(city));
    }
    

    您也可以对数组执行相同的操作,但是您需要声明一个新数组,您将根据类似的城市名称添加计数。此方案的相应数据结构为HashMap

答案 1 :(得分:0)

最后用二维数组完成了 我的代码是

import java.util.Arrays;

/**
 *
 * @author Chirag
 */
public class Sampleh {

    private static final String[][] $DATA = new String[][]{
        {"New Delhi", "5000"},
        {"Chennai", "4300"},
        {"Goa", "2940"},
        {"New Delhi", "2003"},
        {"Kolkata", "8904"},
        {"Kerala", "8972"},
        {"New Delhi", "8922"},
        {"Chennai", "8217"},
        {"New Delhi", "2462"},
        {"Kolkata", "5564"},
        {"Kerala", "9934"},
        {"New Delhi", "100"},
        {"Kolkata", "892"},
        {"Kerala", "9406"},
        {"New Delhi", "2003"},
        {"Chennai", "1049"}
    };

    private static String[][] $result = new String[50][2];

    private static void action1() {
        outer:
        for (int i = 0; i < $DATA.length; i++) {
            String curCity = $DATA[i][0];
            Integer curProd = Integer.valueOf($DATA[i][1]);
            for (String[] city : $result) {
                if (city[0] == null) {
                    //for this loop
                    continue;
                }
                if (city[0].equals(curCity)) {
                    //for outer loop;
                    continue outer;
                }
            }
            for (int k = i; k < $DATA.length; k++) {
                if ($DATA[k][0].equals(curCity)) {
                    String n = $DATA[k][1];
                    if (n == null) {
                        n = "0";
                    }
                    curProd += Integer.valueOf(n);
                }
            }
            $result[i][0] = curCity;
            $result[i][1] = "" + curProd;
        }
        {
            //this code removes nulls from result array
            String[][] temp; //a temparory 2d array
            int counter = 0; //counts the num of values excluding nulls
            for (String[] res : $result) {
                if (res[0] == null) {
                    continue;
                }
                ++counter;
            }
            temp = new String[counter][2]; //redefine the temporary 2d array with size of values
            counter = 0;
            for (String[] res : $result) {
                if (res[0] == null) {
                    continue;
                }
                temp[counter][0] = res[0];
                temp[counter][1] = res[1];
                counter++;
            }
            $result = temp; //copy all values from temporary array to global result array
        }

        //prints the final array which is not sorted
        for (String[] r : $result) {
            System.out.println("City " + r[0] + " have " + r[1] + " products");
        }
    }

    private static void action2() {
        String[] cities = new String[$result.length];
        int[] products = new int[$result.length];
        int[] prodCopy = new int[$result.length];

        for (int i = 0; i < $result.length; i++) {
            cities[i] = $result[i][0];
            prodCopy[i] = Integer.valueOf($result[i][1]);
            products[i] = Integer.valueOf($result[i][1]);
        }

        Arrays.sort(products);

        for (int i = 0; i < products.length; i++) {
            int counter = -1;
            for (int p : prodCopy) {
                ++counter;
                if (p == products[i]) {
                    break; //breaks the loop to continue to other code without increasing counter
                }
            }
            $result[i][0] = cities[counter];
            $result[i][1] = "" + prodCopy[counter];
        }

        System.out.println("\nprinting full list sorted as counting of product");
        for (String[] k : $result) {
            System.out.println("City: " + k[0] + " have products " + k[1]);
        }

        int loc = $result.length - 2;
        System.out.println();
        System.out.println("Second highest City is '" + $result[loc][0] + "' having products '" + $result[loc][1] + "'");
        System.out.println();
    }

    public static void main(String... $results) {
        action1();
        action2();
    }
}