2020年12月21日星期一

Why am I getting a out of bounds error even though I am changing the value of the for loop?

I am trying to assign a value to an index but I am getting an out of bounds error. I have looked at previous questions but none of theme address this particular problem with a for loop. I thought I addressed the problem when I changed the value of the iterator to make sure it doesn't go out of bounds. Below is the code snippet: The location of the error is indicated below by the comment.

for (int i = 0; i < n; i++) {              for (int j = tickets.size() - 1; j >= 0; j--) {                  System.out.println(tickets.get(j) + ":" + j); // error here                  if (tickets.get(j) <= customers.get(i)) {                      finalTickets.set(j, tickets.get(j));                      tickets.remove(j);                      break;                  }              }  //          pw.println(finalTickets);          }  

Below is the full code:

import java.io.*;  import java.util.*;    public class ConcertTickets {        public static void main(String[] args) throws IOException {            BufferedReader r = new BufferedReader(new InputStreamReader(System.in));          PrintWriter pw = new PrintWriter(System.out);          StringTokenizer st = new StringTokenizer(r.readLine());          int n = Integer.parseInt(st.nextToken());          int m = Integer.parseInt(st.nextToken());            ArrayList<Integer> tickets = new ArrayList<>(n);          StringTokenizer ticketInput = new StringTokenizer(r.readLine());          for (int i = 0; i < n; i++) {              tickets.add(Integer.parseInt(ticketInput.nextToken()));          }            ArrayList<Integer> finalTickets = new ArrayList<>(m);          for (int i = 0; i < m; i++) {              finalTickets.add(-1);          }            ArrayList<Integer> customers = new ArrayList<>(m);          StringTokenizer customersInput = new StringTokenizer(r.readLine());          for (int i = 0; i < m; i++) {              customers.add(Integer.parseInt(customersInput.nextToken()));          }                              Collections.sort(tickets);          for (int i = 0; i < n; i++) {              for (int j = tickets.size() - 1; j >= 0; j--) {                  System.out.println(tickets.get(j) + ":" + j);                  if (tickets.get(j) <= customers.get(i)) {                      finalTickets.set(j, tickets.get(j));                      tickets.remove(j);                      break;                  }              }  //          pw.println(finalTickets);          }          pw.println(finalTickets);               pw.close();      }    }    

This is the error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4      at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)      at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)      at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)      at java.base/java.util.Objects.checkIndex(Objects.java:373)      at java.base/java.util.ArrayList.get(ArrayList.java:427)      at ConcertTickets.main(ConcertTickets.java:36)  

Is there something I am doing wrong, also please let me know if I need to have additional information. Thanks in advance.

https://stackoverflow.com/questions/65402343/why-am-i-getting-a-out-of-bounds-error-even-though-i-am-changing-the-value-of-th December 22, 2020 at 09:57AM

没有评论:

发表评论