【问题标题】:Adding unique values to java 2d arrays + how to sort them?向java 2d数组添加唯一值+如何对它们进行排序?
【发布时间】:2016-02-04 04:20:49
【问题描述】:

我需要你的帮助!我有一个班级作业,我必须从 Excel 列表中获取销售数据,将其导入 java,然后输出一个新列表,该列表对产品 ID 和每种产品的销售单位数量进行了排序。这意味着如果原始列表有 5x 产品 ID“1003”,则最终列表应仅列出 1x 产品 ID“1003”,并将 5 个不同的单位销售额合并为一个总数。

编辑:我可能应该显示原始 excel 列表的样子:

ProductID......单位

10004............. 4

10002...................... 2

10004 .... 3

10008 .... 6

10009 .............3

等等等等。所以基本上只需将重复的 ID 组合起来,然后将它们的总销量相加。

我现在被困在两件事上。我正在尝试为最终列表创建一个新的多维数组,但我无法添加唯一的 productID。我尝试使用 for 循环执行此操作,但它仍会添加多个 productID。

另外,我尝试按 Product ID 列按升序对最终数组进行排序,但无法正常工作。

我已将 cmets 与 CAPS LOCK 一起显示这两个问题在哪里!

谢谢大家!!!

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;


public class Project1{


public static void main(String[] args) {



Project1 obj = new Project1();
    obj.run();

  }

  public void run() {

    String csvFile = "C:/Users/Jenny/Documents/Classwork/SalesData.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    int counter = 0;
    int salesDataArrayCounter = 0;
    int [][] finalSalesData = new int[200][2];
    System.out.println("Product ID\tUnits");  //Headers for original Data
    try {

        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

                // use comma as separator
            String[] salesData = line.split(cvsSplitBy);

            while (counter>0)                //this allows me to skip first line of data (It's just text, I need the numbers)
            {

            System.out.println(salesData[0] + " \t\t " + salesData[1]);
            int productID = Integer.parseInt(salesData[0]);
            int units = Integer.parseInt(salesData[1]);



            search:   //THIS IS WHERE MY FIRST TROUBLE LIES
            {
            for (int i = 0; i < finalSalesData.length; i++)
            {
                if(finalSalesData[i][0]==(productID))       //Should let me find duplicate product ID's and only add the Units to a pre-existing index
                {
                    finalSalesData[i][1] = finalSalesData[i][1] + units;
                    break search;
                }
                else                                        //If productID is unique, add new product ID and units to a new slot in the new array
                {
                    finalSalesData[salesDataArrayCounter][0] = productID;
                    finalSalesData[salesDataArrayCounter][1] = units;
                    salesDataArrayCounter++;
                    break search;
                }
            }
            }

            break;
            }
            counter++;

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    System.out.println("\n\nSorted Sales Data");
    System.out.println("\nProductID\tUnits");

    //HOW DO I SORT THIS DATA IN ASCENDING ORDER OF PRODUCT IDS?

    for (int q = 0; q < finalSalesData.length; q++)
    {
        for (int j = 0; j < finalSalesData[q].length; j++)
        {
            System.out.print(finalSalesData[q][j] +"\t\t");
        }
        System.out.println("");
    }

    System.out.println("Done");
  }

}

【问题讨论】:

  • 您可以使用已排序的Map 对键进行排序,然后将其所有订单的总和作为值。 2 只鸟 - 1 块石头。如果你的任务可以接受的话。

标签: java arrays sorting


【解决方案1】:

我会使用 SortedMap 来存储结果,因为它默认按键升序排序。这应该在循环之前定义:

    SortedMap<Integer,Integer> sortedFinalSalesData = new TreeMap<Integer,Integer>();

之后

int 单位 = Integer.parseInt(salesData[1]);

你可以这样实现:

        Integer currentUnits = sortedFinalSalesData.get(productID);
        sortedFinalSalesData.put(productID, currentUnits != null ? units + currentUnits : units);

如果这没有意义,请查找三级运算符,因为您应该在学校作业中学习一些东西 (LOL)!

【讨论】:

  • 谢谢詹妮弗,很高兴我能帮上忙
【解决方案2】:

您是否需要使用二维数组?创建一个包含销售信息和产品 ID 的类可能更有意义。

  1. 制作一个空的产品对象列表。
  2. 对于 CSV 中的每个项目,检查列表以查看产品 ID 是否已存在。如果是,则将产品信息添加到现有对象。如果没有,请将新产品对象添加到您的列表中。

用于排序:

编写自定义比较方法并使用内置排序功能,如下所示:Sort ArrayList of custom Objects by property

代码中的第二个 while 循环作为 if 语句会更有意义...或者只是从 1 开始计数器。

【讨论】:

    猜你喜欢
    • 2019-09-28
    • 1970-01-01
    • 2010-09-06
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多