I cannot find my error in my program, which is supposed to add and subtract ints to a queue and then print out the average and max of each queue over 500 minutes. I keep getting an out of bounds exception error but cannot solve the problem. Any help determining where my issue is would be greatly appreciated!
import java.util.*;
import java.util.Random;
import java.util.LinkedList;
import java.util.ArrayList;
public class AirportQueue {
public static int longestWaitTime = 0;
public static Random rand = new Random(3);
public static int partySize;
public static Random randQueue = new Random(2);
public static Queue<Integer> airportQ1 = new LinkedList<>();
public static Queue<Integer> airportQ2 = new LinkedList<>();
public static Queue<Integer> airportQ3 = new LinkedList<>();
public static ArrayList[] minute = new ArrayList[500];
public static ArrayList[] secondMinute = new ArrayList[250];
public static ArrayList<Integer> q1 = new ArrayList(0);
public static ArrayList<Integer> q2 = new ArrayList(0);
public static ArrayList<Integer> q3 = new ArrayList(0);
public static int avgQ1 = 0;
public static int avgQ2 = 0;
public static int avgQ3 = 0;
public void airportSimulation() {
for (int i = 0; i < 500; i++) {
if ((i % 2) == 0) { // add a party to a queue each minute
if ((airportQ1.size() < airportQ2.size()) && (airportQ1.size() < airportQ3.size())) {
partySize = rand.nextInt();
airportQ1.add(partySize);
q1.add(partySize);
} else if ((airportQ2.size() < airportQ1.size()) && (airportQ2.size() < airportQ3.size())) {
partySize = rand.nextInt();
airportQ2.add(partySize);
q2.add(partySize);
} else {
partySize = rand.nextInt();
airportQ3.add(partySize);
q3.add(partySize);
}
int queueSelected = randQueue.nextInt();
if (queueSelected == 0) {
if (airportQ1.isEmpty()) {
System.out.println("The first queue is empty.");
} else {
airportQ1.remove();
}
} else if (queueSelected == 1) { // remove a party every 2 minutes
if (airportQ2.isEmpty()) {
System.out.println("The second queue is empty.");
} else {
airportQ2.remove();
}
} else {
if (airportQ3.isEmpty()) {
System.out.println("The third queue is empty.");
} else {
airportQ3.remove();
}
}
} else {
if ((airportQ1.size() < airportQ2.size()) && (airportQ1.size() < airportQ3.size())) {
partySize = rand.nextInt();
airportQ1.add(partySize);
q1.add(partySize);
} else if ((airportQ2.size() < airportQ1.size()) && (airportQ2.size() < airportQ3.size())) {
partySize = rand.nextInt();
airportQ2.add(partySize);
q2.add(partySize);
} else {
partySize = rand.nextInt();
airportQ3.add(partySize);
q3.add(partySize);
}
}
}
// num of people in queue
for (int k = 0; k < airportQ1.size(); k++) {
int q1Total = 0;
avgQ1 = (q1Total + q1.get(k)) / 500;
}
for (int k = 0; k < airportQ2.size(); k++) {
int q1Total = 0;
avgQ2 = (q1Total + q2.get(k)) / 500;
}
for (int k = 0; k < airportQ3.size(); k++) {
int q1Total = 0;
avgQ3 = (q1Total + q3.get(k)) / 500;
}
// Max value for each queue
int max1 = q1.get(0);
for (int i = 0; i < q1.size(); i++) {
int current1 = q1.get(i);
if (current1 > max1) {
max1 = current1;
}
}
int max2 = q2.get(0);
for (int i = 0; i < q2.size(); i++) {
int current2 = q2.get(i);
if (current2 > max2) {
max2 = current2;
}
}
int max3 = q3.get(0);
for (int i = 0; i < q3.size(); i++) {
int current3 = q3.get(i);
if (current3 > max3) {
max3 = current3;
}
}
System.out.println("The average number of people in the first "
+ "Airport Security queue is " + avgQ1);
System.out.println("The average number of people in the second "
+ "Airport Security queue is " + avgQ2);
System.out.println("The average number of people in the third "
+ "Airport Security queue is " + avgQ3);
System.out.println("The maximum number of people in the first "
+ "queue at any given time was " + max1);
System.out.println("The maximum number of people in the first "
+ "queue at any given time was " + max2);
System.out.println("The maximum number of people in the first "
+ "queue at any given time was " + max3);
}
public static void main(String[] args) {
AirportQueue testAirport = new AirportQueue();
testAirport.airportSimulation();
}
}
Stacktrace:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at AirportQueue.airportSimulation(AirportQueue.java:93)
at AirportQueue.main(AirportQueue.java:134)
Line in question:
int max1 = q1.get(0);
int q1Total = 0; avgQ1 = (q1Total + q1.get(k))/500;hint (every avgQ1 = the last element/500).int max1 = q1.get(0);which means the size ofq1is0.q1.add()call, which is understandable given that there are 4 other paths it could have taken through the first series ofifblocks. (Indeed ifiis odd, it won't even go through the if-blocks at all. So what happens wheniis 1 and nothing has been added toq1on the previousi==0pass through the loop?