简体   繁体   English

如何在 2D Arraylist 中找到最大值/秒?

[英]How to find maximum value/s in a 2D Arraylist?

I'm trying to rewrite an old program by using Arraylists instead of just arrays.我正在尝试使用 Arraylists 而不仅仅是 arrays 来重写旧程序。 The program was a database of the top 5 video game sales in 2017, and one of the methods the program does is finding the maximum values per location (4 locations in all: NA, EU, JP, & Other Sales).该程序是 2017 年电子游戏销量前 5 名的数据库,该程序执行的方法之一是查找每个位置的最大值(总共 4 个位置:北美、欧盟、日本和其他销售)。

(Note: There are 5 columns of sales in all (NA, EU, JP, Other Sales, Global Sales), and I only wanted the method to find the maximum values in the first four columns.) (注意:总共有 5 列销售额(NA、EU、JP、Other Sales、Global Sales),我只希望该方法在前四列中找到最大值。)

These are snippets of the 2D array and its corresponding method for finding the maximum values:这些是二维数组的片段及其查找最大值的相应方法:

double[][] Sales ={ {41.49,29.02,3.77,8.46,82.74},{29.08,3.58,6.81,0.77,40.24},
        {15.85,12.88,3.79,3.31,35.83},{15.75,11.01,3.28,2.96,33},
        {11.27,8.89,10.22,1,31.38} };


// maximum value
public static void hiSales(double[][] sales) {
    double max1;
    double[] max = new double[sales.length];
    for(int j=0; j<sales[0].length-1;j++) { //WILL WORK AS LONG AS IT IS SQUARE MATRIX
        
        for (int i = 0; i < sales.length; i++) {
            max1 = sales[0][j];
            if (sales[i][j] > max1) {
                max1 = sales[i][j];
            }
            if (i == sales.length - 1) {
                max[j] = max1;
            }
        }
    }
    System.out.println("\n                | HIGHEST SALES PER LOCATION |");
    System.out.println("\n | NA Sales |    | EU Sales |    | JP Sales |   "
            + "| Other Sales |   \n");
    System.out.println("— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —\n");
    System.out.print("     ");
    for(int i=0; i<max.length-1; i++) {
        System.out.print(max[i] + "\t");
        System.out.print("     ");
    } 
    System.out.println("\n");
    System.out.println("— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —");
}

Now, I've converted the 2D array into a 2D Arraylist, and the method cannot function anymore, which is the problem I would like to address.现在,我已经将二维数组转换为二维 Arraylist,并且该方法不再是 function,这是我想解决的问题。

This is the new 2D Arraylist I've converted:这是我转换的新 2D Arraylist:

ArrayList<VGSdata> sales = new ArrayList<>();
VGSdata wii = new VGSdata(41.49,29.02,3.77,8.46,82.74);
VGSdata bros = new VGSdata(29.08,3.58,6.81,0.77,40.24);
VGSdata kart = new VGSdata(15.85,12.88,3.79,3.31,35.83);
VGSdata res = new VGSdata(15.75,11.01,3.28,2.96,33);
VGSdata prb = new VGSdata(11.27,8.89,10.22,1,31.38);
    
sales.add(wii);
sales.add(bros);
sales.add(kart);
sales.add(res);
sales.add(prb);

So.. how will the method be rewritten in order for it to utilize the data in the ArrayList while still performing the same function?那么..如何重写该方法以使其利用ArrayList中的数据,同时仍执行相同的 function?

If you do not want to rewrite the hiSales method then you could create a 2d array from the arraylist and then pass it on to the hiSales method.如果您不想重写hiSales方法,则可以从 arraylist 创建一个二维数组,然后将其传递给hiSales方法。

double[][] dataArray = new double[sales.size()][4];
int i=0;
for(VGSdata data : sales) {
    dataArray[i][0] = data.displayNA();
    dataArray[i][1] = data.displayEU();
    dataArray[i][2] = data.displayJP();
    dataArray[i][3] = data.displayOS();

    i++;
}

// now invoke hiSales
hiSales(dataArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM