fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int[] a = { 2, 3, 4, 5 };
  13. int b = a.length - 1;
  14. // cria uma lista de int
  15. List<Integer> arr2 = new ArrayList<Integer>();
  16. int i = 0;
  17. while (i < b) {
  18. if (a[i] % 2 != 0) {
  19. // Adiciona a[i] a lista arr2
  20. arr2.add(a[i]);
  21. // Imprime o item i da lsta arr2
  22. System.out.println((int) arr2.get(i)); // vai dar erro aqui
  23. }
  24. i++;
  25. }
  26. System.out.println(arr2);
  27. }
  28. }
Runtime error #stdin #stdout #stderr 0.05s 2184192KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
	at java.util.ArrayList.rangeCheck(ArrayList.java:653)
	at java.util.ArrayList.get(ArrayList.java:429)
	at Ideone.main(Main.java:22)