gpt4 book ai didi

java - ArrayList 在 for 循环中的行为不符合预期

转载 作者:行者123 更新时间:2023-12-02 12:14:51 25 4
gpt4 key购买 nike

import java.util.*;

public class elevator_EXPERIMENT {
public static void main(String[] args) {

ArrayList<Integer> UpArrayList = new ArrayList<Integer>();
ArrayList<Integer> DownArrayList = new ArrayList<Integer>();

int[] Floor = new int[12];
for (int i = 0; i < Floor.length; i++) {
Floor[i] = i + 1;
}
while (UpArrayList.size() != 8) {
if (Math.random() > 0.5) {
Random x = new Random();
int UpRandom = (x.nextInt(11 - 1) + 1) + 1;
Random x1 = new Random();
int UpRandom1 = (x1.nextInt(11 - 1) + 1) + 1;
if (UpRandom != UpRandom1 && !UpArrayList.contains(Floor[UpRandom])) {
UpArrayList.add(UpRandom1);
}
}
}
while (UpArrayList.size() != 8) {
Random x = new Random();
int UpRandom = (x.nextInt(11 - 1) + 1) + 1;
while (UpArrayList.contains(Floor[UpRandom])) {

}
}
System.out.print("Unsorted floors going up ");
for (int i = 0; i < UpArrayList.size(); i++) {
System.out.print(UpArrayList.get(i) + " ");
}
System.out.println();
while (DownArrayList.size() != 5) {
if (Math.random() > 0.5) {
Random y = new Random();
int DownRandom = (y.nextInt(11 - 1) + 1) + 1;
if (DownArrayList.contains(Floor[DownRandom])) {
Random y1 = new Random();
int DownRandom1 = (y1.nextInt(11 - 1) + 1) + 1;
if (DownRandom != DownRandom1 && !DownArrayList.contains(Floor[DownRandom1])) {
DownArrayList.add(DownRandom1);
}
} else {
DownArrayList.add(Floor[DownRandom]);
}
}
}
System.out.print("\nUnsorted floors going down ");
for (int i = 0; i < DownArrayList.size(); i++) {
System.out.print(DownArrayList.get(i) + " ");
}
Collections.sort(UpArrayList);
System.out.print("\n\n" + "Sorted floors going up ");
for (int i = 0; i < UpArrayList.size(); i++) {
System.out.print(UpArrayList.get(i) + " ");
}
Collections.sort(DownArrayList, Collections.reverseOrder());
System.out.print("\n\n" + "Sorted floors going down ");
for (int i = 0; i < DownArrayList.size(); i++) {
System.out.print(DownArrayList.get(i) + " ");
}
System.out.println();
for (int i = 0; i < 8; i++) {
int floornumber = 1;
System.out.println("\nStarting at floor " + floornumber);
floornumber++;
System.out.println("Going up: now at floor " + UpArrayList.get(floornumber));
System.out.println("Stopping at floor " + UpArrayList.get(floornumber) + " for 3 seconds -> 1,2,3");
SleepTimer(3000);
}
}

public static void SleepTimer(int time) {
try {
Thread.sleep(time);
} catch (Exception e) {
System.out.println("Something went wrong");
}
}
}

您好,请看一下SleepTimer方法上面的for循环。我有两个问题。我的目标是创建一个电梯txt模拟器,电梯有12层。我创建了两个 Arraylist:一个包含 8 个随机唯一楼层,另一个包含 5 个随机唯一楼层,分别标记为 UpArrayList 和 DownArrayList。然后我对它们进行排序。然后我应该创建一个类似于以下内容的 txt 提示:

2 3 4 6 7 8 10 11 Sorted ArrayList

        Starting at floor 1
Going up: now at floor 2
Stopping at floor 2 for 3 seconds  1, 2, 3

Starting at floor 2
Going up: now at floor 3
Stopping at floor 3 for 3 seconds  1, 2, 3

Starting at floor 3
Going up: now at floor 4
Stopping at floor 4 for 3 seconds  1, 2, 3

Starting at floor 4
Going up: now at floor 5
Going up: now at floor 6
Stopping at floor 6 for 3 seconds  1, 2, 3

Starting at floor 6
Going up: now at floor 7
Stopping at floor 7 for 3 seconds  1, 2, 3

Starting at floor 7
Going up: now at floor 8
Stopping at floor 8 for 3 seconds  1, 2, 3

基本上我想要 for 循环(在程序末尾)的提示格式

Starting at Floor1
Going up: now at floor[FirstElementofUpArrayList]
Stopping at floor[FirstElementofUpArrayList" for 3 seconds -> 1,2,3
Starting at floor[SecondElementofUpArrayList]
Going up: now at floor[SecondElementofUpArrayList] for 3 seconds -> 1,2,3

直到到达 UpArrayList 的最后一个元素。

最佳答案

“ArrayList 在 forloop 中的行为不符合预期”

它什么也没说,但是看看你的实现,我猜你的问题是 java.lang.IndexOutOfBoundsException 这是因为列表的大小是 8 并且元素的索引从 0 到 7。最后一个循环的最后一次迭代正在调用 for UpArrayList.get(8),但没有第9个元素。改变

UpArrayList.get(floornumber);

UpArrayList.get(i);

关于java - ArrayList 在 for 循环中的行为不符合预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46265062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com