我有一个 2D 数组列表,其中每行都有 1-4 队随机洗牌。我为每个团队都有一个单独的数组列表,我想在其中存储其索引。例如,如果我的 2D 数组列表如下所示:
2、3、1、4
2、1、4、3
4、2、1、3
我的 team1 索引的数组列表应该是 [2,1,2],team 2 应该是 [0, 0, 1],team 3 应该是 [1 ,3, 3],而 team 4 应该是 [3, 2, 0]。
但是,我在实现一个循环时遇到了麻烦,该循环可以识别每个数字的索引并将其添加到其相应的数组列表中,然后移动到下一行。我最终得到的值为 -1(意味着请求的号码不存在)。有人可以看看我到目前为止所拥有的并提出一些帮助吗?
只是一点背景知识(因为我这里还有其他一些东西):我存储洗牌后的数字的数组列表是动态的。仅当温度输入高于 32 时才会添加打乱后的数字。如果连续 3 次输入温度低于 32,则循环终止。
package practice;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
public class everythingTogether {
public static void main(String[] args) {
//Create Arrays
//Store Temperatures
ArrayList < Double > allTemperatures = new ArrayList < > ();
ArrayList < Double > daysPlayed = new ArrayList < > ();
List < Integer[] > teamMatches = new ArrayList < > ();
//make array list for team index (0,1 home| 2,3 away)
ArrayList < Integer > homeOrAway1 = new ArrayList < > ();
ArrayList < Integer > homeOrAway2 = new ArrayList < > ();
ArrayList < Integer > homeOrAway3 = new ArrayList < > ();
ArrayList < Integer > homeOrAway4 = new ArrayList < > ();
//start loop for temp and datat collection
for (int j = 0;; j++) {
Integer[] teamNums = new Integer[] {
1,
2,
3,
4
};
//scanner for temperature
Scanner getTemp = new Scanner(System.in);
System.out.println("What is today's temperature?");
double temp = getTemp.nextDouble();
allTemperatures.add(temp);
//checking temperature and if can play
if (j < 2) {
if (temp < 32) {
System.out.println("It is too cold to play.");
} else {
Collections.shuffle(Arrays.asList(teamNums));
teamMatches.add(teamNums);
daysPlayed.add(temp);
}
}
//after day 0 and 1 check if season can be over
else if (j >= 2) {
if (temp < 32) {
System.out.println("It is too cold to play.");
//check if season over
double day1Temp = allTemperatures.get(j);
double day2Temp = allTemperatures.get((j - 1));
double day3Temp = allTemperatures.get((j - 2));
if (day1Temp < 32 & day2Temp < 32 & day3Temp < 32) {
System.out.println("The season is over");
break;
}
} else {
Collections.shuffle(Arrays.asList(teamNums));
teamMatches.add(teamNums);
daysPlayed.add(temp);
}
}
}
System.out.println("_______________________");
//get home teams and away teams
for (int i = 0; i < teamMatches.size(); i++) {
int team1Status = teamMatches.indexOf(1);
int team2Status = teamMatches.indexOf(2);
int team3Status = teamMatches.indexOf(3);
int team4Status = teamMatches.indexOf(4);
homeOrAway1.add(team1Status);
homeOrAway2.add(team2Status);
homeOrAway3.add(team3Status);
homeOrAway4.add(team4Status);
}
System.out.println(homeOrAway1);
System.out.println(homeOrAway2);
System.out.println(homeOrAway3);
System.out.println(homeOrAway4);
}
}
最佳答案
我相信您遇到的错误是由于此循环造成的:
for (int i = 0; i < teamMatches.size(); i++) {
int team1Status = teamMatches.indexOf(1);
int team2Status = teamMatches.indexOf(2);
int team3Status = teamMatches.indexOf(3);
int team4Status = teamMatches.indexOf(4);
homeOrAway1.add(team1Status);
homeOrAway2.add(team2Status);
homeOrAway3.add(team3Status);
homeOrAway4.add(team4Status);
}
请记住,这是您的声明:
List < Integer[] > teamMatches
teamMatches
是一个整数数组的列表。因此,当您调用时:
int team1Status = teamMatches.indexOf(1);
你没有得到你认为的那样。这就是 teamMatches 的实际含义:
你真正想要的是这样的:
for (int i = 0; i < teamMatches.size(); i++) {
Integer[] ints = teamMatches.get(i);
for (int j = 0; j < ints.length; j++) {
if (ints[j] == 1) homeOrAway1.add(j);
else if (ints[j] == 2) homeOrAway2.add(j);
else if (ints[j] == 3) homeOrAway3.add(j);
else if (ints[j] == 4) homeOrAway4.add(j);
}
}
编辑:我要添加一个解释,所以请稍等。
teamMatches 是一个整数数组列表。该数组表示 1,2,3,4(已打乱),本质上是游戏的表示。由于有一个列表,因此 teamMatches 是一个游戏列表。
Integer[] ints = teamMatches.get(i)
是你真正想要的。现在,变量 ints
是一个单机游戏。从这个游戏中,您可以提取有关每个团队位置的信息(不确定术语,抱歉)。
内循环的其余部分应该是不言自明的。它循环遍历整数数组(球队的位置),并根据索引处的值将当前索引添加到 homeOrArray1/2/3/4。
注意:我强烈建议永远不要使用整数数组,除非有充分的理由。我相信你真正想要的是 List<Integer>
因为现在你可以做 ints.indexOf(1/2/3/4);
而不是 for 循环。
显然,我不知道你的程序的规范,但假设你被允许重新定义 teamMatches
,在我看来,下面的代码更简洁。
List<List<Integer>> teamMatches = new ArrayList<>();
...
现在您的整个 for 循环可以替换为:
for (List<Integer> teamPositions : teamMatches) {
homeOrAway1.add(teamPositions.indexOf(1));
homeOrAway2.add(teamPositions.indexOf(2));
//...
}
关于java - 如何从 2D 数组列表获取和存储索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53364497/