Java—基于第一列组织二维数组的问题
我正在尝试组织一个Java—基于第一列组织二维数组的问题,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我正在尝试组织一个2D数组,其中包含游戏的标题及其评级: 我有一个2D数组如下: 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 Nomopoly 2 Nomopoly 3 Nomopoly 5 Pac-Dude 5 Pac-Dude 1 Pac-Dude 5 Settlers of
2D数组
,其中包含游戏的标题及其评级:
我有一个2D数组
如下:
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
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
在按第一列字母顺序对2D数组
排序后,我想将数组拆分为多个2D数组
,如下所示:
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
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
我不确定如何分离
2D数组
。如果我能够将2D数组
与游戏和数组分离成数组,那么就更容易找到游戏的平均评分,这就是我想要看到的。我认为您最好使用一个数据结构,而不是2D数组,也许是树映射
。TreeMap
的好处在于它根据键或本例中的第一列按字母顺序自动排序
因此,构建这些数据的方式如下所示:
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
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
Nomopoly:[2,3,5]吃豆人:[5,1,5]
卡坦定居者:[5]
超级路易吉星球:[3,4]
然后,您所要做的就是将输入解析到结构中,也许可以使用
String.split()
。当您想要显示它时,迭代地图的键和列表的元素,并将其聚合为您想要的格式。我不确定是否需要为此项目/任务拆分数组。但我认为有一种更简单的方法,不需要将原始数组拆分为更多的数组。您只需要在主数组中找到标题,然后计算标题的平均评分。我编写了一个示例,但没有进行测试。我希望它能给你一些想法
import java.util.Scanner;
公开课游戏{
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
}
私有字符串数据[];
私有整数大小;
私人int最后一行;
游戏(){
数据=新字符串[100][2];
尺寸=0;
最后一行=0;
}
公共无效addTitle(){
扫描仪输入=新扫描仪(System.in);
字符串标题;
内部评级=-1;
System.out.print(“输入游戏标题:”);
title=input.next();
系统输出打印(“输入“+标题+”:)的评级;
评级=输入。nextInt();
而(评级>5 | |评级<0){
System.out.println(“输入无效。请输入0到5之间的评级”);
评级=输入。nextInt();
}
数据[最后一行][0]=标题;
数据[最后一行][1]=字符串.valueOf(额定值);
这是最后一行++;
这个.size++;
}
公共双getAverage(字符串键){
int额定值_计数=0;
整数=0;
双倍平均;
对于(int i=0;i
}您是如何初始化阵列的?