Java 2D数组逻辑实现

Java 2D数组逻辑实现,java,arrays,sorting,multidimensional-array,Java,Arrays,Sorting,Multidimensional Array,我今天有个问题,试过了,但没有找到相应的解决方案。 问题是: 假设我们有一个包含城市名称和产品总数的2D数组。城市的名字可以在列表中重复 现在我们要做的是 计算一个城市中销售的产品数量,并将其保存在另一个2d数组中,该数组具有唯一的城市名称和销售的产品总数 打印第二个销售产品最多的城市 可供考虑的样本清单如下: private static String[][] arr = new String[][]{ {"New Delhi", "5000"}, {"Chen

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

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

现在我们要做的是

  • 计算一个城市中销售的产品数量,并将其保存在另一个2d数组中,该数组具有唯一的城市名称和销售的产品总数
  • 打印第二个销售产品最多的城市
  • 可供考虑的样本清单如下:

    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"}
        };
    

    注意:我已经用地图界面试过这个问题了。仅使用多维数组查找解决方案

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

  • 迭代数组
  • 如果key存在,则获取key的值并使用new递增计数 价值观
  • 如果键不存在,则按下该值
  • 基本思路如下:

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

    您也可以对数组执行相同的操作,但需要声明一个新数组,在该数组中,您将根据相似的城市名称添加计数。此场景的适当数据结构为
    HashMap

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

  • 迭代数组
  • 如果key存在,则获取key的值并使用new递增计数 价值观
  • 如果键不存在,则按下该值
  • 基本思路如下:

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

    您也可以对数组执行相同的操作,但需要声明一个新数组,在该数组中,您将根据相似的城市名称添加计数。此场景的适当数据结构是
    HashMap

    最终使用2d数组完成 我的代码是

    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();
        }
    }
    
    导入java.util.array;
    /**
    *
    *@作者希拉格
    */
    公共类样本{
    私有静态最终字符串[][]$DATA=新字符串[][]{
    {“新德里”,“5000”},
    {“钦奈”,“4300”},
    {“果阿”,“2940”},
    {“新德里”,“2003”},
    {“加尔各答”,“8904”},
    {“喀拉拉邦”,“8972”},
    {“新德里”,“8922”},
    {“钦奈”,“8217”},
    {“新德里”,“2462”},
    {“加尔各答”,“5564”},
    {“喀拉拉邦”,“9934”},
    {“新德里”,“100”},
    {“加尔各答”,“892”},
    {“喀拉拉邦”,“9406”},
    {“新德里”,“2003”},
    {“钦奈”,“1049”}
    };
    私有静态字符串[][]$result=新字符串[50][2];
    私有静态void action1(){
    外部:
    对于(int i=0;i<$DATA.length;i++){
    字符串curCity=$DATA[i][0];
    整数curProd=Integer.valueOf($DATA[i][1]);
    for(字符串[]城市:$result){
    如果(城市[0]==null){
    //对于这个循环
    继续;
    }
    如果(城市[0]。等于(城市){
    //用于外环;
    继续对外开放;
    }
    }
    对于(int k=i;k<$DATA.length;k++){
    如果($DATA[k][0]。等于(curCity)){
    字符串n=$DATA[k][1];
    如果(n==null){
    n=“0”;
    }
    curProd+=整数值(n);
    }
    }
    $result[i][0]=curCity;
    $result[i][1]=“”+curProd;
    }
    {
    //此代码从结果数组中删除空值
    字符串[][]临时;//临时数组
    int counter=0;//计算不包含空值的值的数量
    for(字符串[]res:$result){
    如果(res[0]==null){
    继续;
    }
    ++计数器;
    }
    temp=new String[counter][2];//用值的大小重新定义临时2d数组
    计数器=0;
    for(字符串[]res:$result){
    如果(res[0]==null){
    继续;
    }
    温度[计数器][0]=res[0];
    温度[计数器][1]=分辨率[1];
    计数器++;
    }
    $result=temp;//将所有值从临时数组复制到全局结果数组
    }
    //打印未排序的最终数组
    for(字符串[]r:$result){
    System.out.println(“城市”+r[0]+“拥有”+r[1]+“产品”);
    }
    }
    私有静态无效操作2(){
    字符串[]城市=新字符串[$result.length];
    int[]产品=新的int[$result.length];
    int[]prodCopy=newint[$result.length];
    对于(int i=0;i<$result.length;i++){
    城市[i]=$result[i][0];
    prodCopy[i]=Integer.valueOf($result[i][1]);
    乘积[i]=整数.valueOf($result[i][1]);
    }
    数组。排序(产品);
    对于(int i=0;i
    终于用2d数组完成了 我的代码是

    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();
        }
    }
    
    导入java.util.array;
    /**
    *
    *@作者希拉格
    */
    公共类样本{
    私有静态最终字符串[][]$DATA=新字符串[][]{