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层。我创建了两个Arraylists:一个包含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的最后一个元素。
答案 0 :(得分:1)
“ArrayList在forloop中的行为不符合预期”
它什么都没说,但看看你的实现我猜你的问题是java.lang.IndexOutOfBoundsException它是因为你的列表大小是8而元素是从0到7的索引。最后一个循环的最后一次迭代是调用UpArrayList.get(8),但没有第9个元素。 变化
UpArrayList.get(floornumber);
到
UpArrayList.get(i);