简体   繁体   English

Java-基于第一列组织2D数组的问题

[英]Java - Issue in organizing 2D Array based on first column

I'm trying to organize a 2D Array which contains of the title of the game and their ratings: 我正在尝试组织一个2D Array ,其中包含游戏标题及其等级:

I have a 2D array like this: 我有一个像这样的2D array

Super Luigi Planet, 4
Nomopoly 2
Pac-Dude 5
Settlers of Catan 5
Super Luigi Planet 3
Nomopoly 5
Pac-Dude 1
Nomopoly 3
Pac-Dude 5

After sorting the 2D Array alphabetically by first column, I want to split the array apart into multiple 2D Arrays like this: 在按第一列按字母顺序对2D Array排序之后,我想将数组分成多个2D Arrays如下所示:

Nomopoly 2
Nomopoly 3
Nomopoly 5

Pac-Dude 5
Pac-Dude 1
Pac-Dude 5

Settlers of Catan 5

Super Luigi Planet 3
Super Luigi Planet 4

I am not sure how I would approach separating the 2D array . 我不确定如何分离2D array If I am able to seperate the 2D array into arrays with the game and array, it would be much easier to find the average rating for the games, which is what I want to see. 如果我能够将2D array与游戏和数组分离成多个数组,那么找到游戏的平均评分会容易得多,这就是我想要看到的。

I think you might be better off using a data structure other than a 2D array, perhaps a TreeMap<String, List<Integer>> . 我认为您最好使用2D数组以外的数据结构,例如TreeMap<String, List<Integer>> The nice thing about TreeMap is that it automatically sorts alphabetically based on the key, or first column in this case. TreeMap的优点在于,它会根据键(在这种情况下为第一列)自动按字母顺序排序。

So the way you would structure this data would look something like this: 因此,您构造数据的方式将如下所示:

Nomopoly: [2, 3, 5] 垄断:[2、3、5]
Pac-Dude: [5, 1, 5] Pac-Dude:[5,1,5]
Settlers of Catan: [5] 卡坦定居者:[5]
Super Luigi Planet: [3, 4] 超级路易吉星球:[3,4]

Then all you have to do is parse the input, perhaps with String.split() , into the structure. 然后,您所要做的就是将输入(可能是使用String.split()解析到结构中。 When you want to display it, iterate over the keys of the map and the elements of the list and aggregate it into the format you want. 当您要显示它时,请遍历地图的键和列表的元素,并将其聚合为所需的格式。

I'm not sure if you are required to split the arrays for this project/assignment. 我不确定是否需要为此项目/任务拆分阵列。 But I think there is an easier approach, that doesn't need to split the original array into more arrays. 但是我认为有一种更简单的方法,不需要将原始数组拆分为更多的数组。 You just need to find the titles in the main array then calculate the rating average for the title. 您只需要在主数组中找到标题,然后计算标题的平均评分即可。 I coded an example, but I didn't test it. 我编写了一个示例,但没有对其进行测试。 I hope it gives you some idea. 我希望它能给您一些想法。

import java.util.Scanner;

public class Games { 公共类游戏{

private String data[][];
private int size;
private int last_line;

Games() {
    data = new String[100][2];
    size = 0;
    last_line = 0;
}

public void addTitle() {
    Scanner input = new Scanner(System.in);
    String title;
    int rating = -1;
    System.out.print("Enter the title of the game:");
    title = input.next();
    System.out.print("Enter the rating of " + title + ":");
    rating = input.nextInt();
    while (rating > 5 || rating < 0) {
        System.out.println("Invalid entry. Please enter the rating from 0 to 5");
        rating = input.nextInt();
    }
    data[last_line][0] = title;
    data[last_line][1] = String.valueOf(rating);
    this.last_line++;
    this.size++;
}

public double getAverage(String key) {
    int rating_count = 0;
    int number = 0;
    double average;
    for (int i = 0; i < size; i++) {
        if (data[i][0].equalsIgnoreCase(key)) {
            rating_count += Integer.parseInt(data[i][1]);
            number++;
        }
    }
    average = rating_count / number;
    return average;
}

public static void main(String[] args) {
    // TODO code application logic here
}

} }

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

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