简体   繁体   中英

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:

I have a 2D array like this:

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:

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 . 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.

I think you might be better off using a data structure other than a 2D array, perhaps a 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.

So the way you would structure this data would look something like this:

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

Then all you have to do is parse the input, perhaps with String.split() , into the structure. 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
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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