Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2

0

I have a problem with this program, when I enter the second FOR this iterates 3 times and the program fails to give me this message:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2     at java.util.ArrayList.rangeCheck (ArrayList.java:657)     at java.util.ArrayList.get (ArrayList.java:433)     at diversion.Main.main (Main.java:100)

Why is this error?

Main Class:

package diversion;

import java.util.ArrayList; import java.util.Objects; import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    ArrayList<Integer> Dias = new ArrayList();
    ArrayList<Integer> Fechas_Montana = new ArrayList();
    ArrayList<Integer> Fechas_Pulpo = new ArrayList();
    ArrayList<Integer> Fechas_Chocadores = new ArrayList();
    ArrayList<Integer> Fechas_Silla = new ArrayList();
    ArrayList<Integer> Fechas_Pendulum = new ArrayList();
    ArrayList<Integer> Fechas_Desorbitador = new ArrayList();

    Jueguitos Montana = new Jueguitos("Montana URSS", Fechas_Montana);
    Jueguitos Pulpo = new Jueguitos("Pulpo", Fechas_Pulpo);
    Jueguitos Chocadores = new Jueguitos("Autitos Chocones", Fechas_Chocadores);
    Jueguitos Pendulum = new Jueguitos("Pendulum", Fechas_Pendulum);
    Jueguitos Desorbirtador = new Jueguitos("Desorbitador", Fechas_Desorbitador);
    Jueguitos Sillas = new Jueguitos("Sillas Voladoras", Fechas_Desorbitador);
    Jueguitos obj_jue = new Jueguitos();

    Laburantes t1 = new Laburantes("Jose Carlos Pepe", "Martinez", 1, "");
    Laburantes t2 = new Laburantes("Cosme", "Fulanito", 2, "");

    /*int indicador1 = 0;
    int indicador2 = 0;
    int indicador3 = 0;
    int indicador4 = 0;
    int indicador5 = 0;
    int indicador6 = 0;*/

/ int d = 501;         for (int i = 0; i < 31; i ++) {             Days.add (d);             d ++;         }         int d2 = 601;         for (int i = 0; i < 30; i ++) {             Days.add (d2);             d2 ++;         } /         Days.add (501);         Days.add (502);         Days.add (503);         Days.add (504);         Dias.add (505);

    for (int h = 0; h < Dias.size(); h++) {
        System.out.println("dia: " + Dias.get(h));
    }
    Fechas_Montana.add(515);
    Fechas_Montana.add(530);
    Fechas_Montana.add(614);
    Fechas_Montana.add(629);
    Fechas_Pulpo.add(518);
    Fechas_Pulpo.add(618);
    Fechas_Chocadores.add(503);
    Fechas_Chocadores.add(511);
    Fechas_Chocadores.add(516);
    Fechas_Chocadores.add(526);
    Fechas_Chocadores.add(529);
    Fechas_Chocadores.add(606);
    Fechas_Chocadores.add(617);
    Fechas_Chocadores.add(621);
    Fechas_Silla.add(515);
    Fechas_Silla.add(530);
    Fechas_Silla.add(614);
    Fechas_Silla.add(629);
    Fechas_Pendulum.add(501);
    Fechas_Pendulum.add(507);
    Fechas_Pendulum.add(515);
    Fechas_Pendulum.add(522);
    Fechas_Pendulum.add(529);
    Fechas_Pendulum.add(605);
    Fechas_Pendulum.add(612);
    Fechas_Pendulum.add(619);
    Fechas_Pendulum.add(626);
    Fechas_Desorbitador.add(501);
    Fechas_Desorbitador.add(601);
    Fechas_Silla.add(515);
    Fechas_Silla.add(530);
    Fechas_Silla.add(614);
    Fechas_Silla.add(629);
    System.out.println(Dias.size());

    for (int i = 0; Dias.size() >= i; i++) {//Recorre los dias
        System.out.println("Buen dia.");
        if (Objects.equals(Dias.get(i), Fechas_Montana.get(i))) {
            System.out.println("La Montana URSS requiere mantenimiento.");
            //indicador1=1;
        }
        if (Objects.equals(Dias.get(i), Fechas_Pulpo.get(i))) {
            System.out.println("El Pulpo requiere mantenimiento.");
            // indicador2=1;
        }
        if (Objects.equals(Dias.get(i), Fechas_Chocadores.get(i))) {
            System.out.println("Los Autitos Chocones requieren mantenimiento.");
            // indicador3=1;
        }
        if (Objects.equals(Dias.get(i), Fechas_Desorbitador.get(i))) {
            System.out.println("El Desorbitador requiere mantenimiento.");
            //indicador4=1;
        }
        if (Objects.equals(Dias.get(i), Fechas_Pendulum.get(i))) {
            System.out.println("El Pendulum requiere mantenimiento.");
            //indicador5=1;
        }
        if (Objects.equals(Dias.get(i), Fechas_Silla.get(i))) {
            System.out.println("Las Sillas Voladoras requieren mantenimiento.");
            //indicador6=1;
        }
        System.out.print("Ingrese una tecla:");
        Scanner scn = new Scanner(System.in);
        String op = scn.nextLine();
    }

}

}

package diversion;

import java.util.ArrayList;

public class Jueguitos {     private String Name;     private ArrayList Mant;

public Jueguitos(String Nombre, ArrayList Mant) {
    this.Nombre = Nombre;
    this.Mant = Mant;
}

public Jueguitos() {
}

/**
 * @return the Nombre
 */
public String getNombre() {
    return Nombre;
}

/**
 * @param Nombre the Nombre to set
 */
public void setNombre(String Nombre) {
    this.Nombre = Nombre;
}

/**
 * @return the Mant
 */
public ArrayList getMant() {
    return Mant;
}

/**
 * @param Mant the Mant to set
 */
public void setMant(ArrayList Mant) {
    this.Mant = Mant;
}

}

package diversion;

public class Laburantes {     private String Name;     private String Surname;     private int Legajo;     Private String Attraction;

public Laburantes(String Nombre, String Apellido, int Legajo, String Atraccion) {
    this.Nombre = Nombre;
    this.Apellido = Apellido;
    this.Legajo = Legajo;
    this.Atraccion = Atraccion;
}

public Laburantes() {
}

/**
 * @return the Nombre
 */
public String getNombre() {
    return Nombre;
}

/**
 * @param Nombre the Nombre to set
 */
public void setNombre(String Nombre) {
    this.Nombre = Nombre;
}

/**
 * @return the Apellido
 */
public String getApellido() {
    return Apellido;
}

/**
 * @param Apellido the Apellido to set
 */
public void setApellido(String Apellido) {
    this.Apellido = Apellido;
}

/**
 * @return the Legajo
 */
public int getLegajo() {
    return Legajo;
}

/**
 * @param Legajo the Legajo to set
 */
public void setLegajo(int Legajo) {
    this.Legajo = Legajo;
}

/**
 * @return the Atraccion
 */
public String getAtraccion() {
    return Atraccion;
}

/**
 * @param Atraccion the Atraccion to set
 */
public void setAtraccion(String Atraccion) {
    this.Atraccion = Atraccion;
}

}

    
asked by Nicolas Martinez 26.05.2018 в 22:09
source

1 answer

1

That error is because you are trying to access a position that does not exist. Your error is located on the following line:

for (int i = 0; Dias.size() >= i; i++) {//Recorre los dias

Given that in this case there are two days, there is only position 0 and 1 and not 2, your for should be

for (int i = 0; Dias.size() > i; i++) {//Recorre los dias
    
answered by 26.05.2018 в 22:16