0

tengo un problema con este programa, cuando ingreso al segundo FOR este itera 3 veces y el programa falla dándome este mensaje:

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)

A que se debe este error ?

Clase Main:

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++) { Dias.add(d); d++; } int d2 = 601; for (int i = 0; i < 30; i++) { Dias.add(d2); d2++; }/ Dias.add(501); Dias.add(502); Dias.add(503); Dias.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 Nombre; 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 Nombre; private String Apellido; private int Legajo; private String Atraccion;

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;
}

}

1 respuesta 1

2

Ese error se debe a que estas tratando de aceder a una posicion que no existe. Tu error se ubica en la siguiente linea:

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

Dado si en este caso existe dos dias, solo existe la posicion 0 y 1 y no la 2, tu for debe quedar

for (int i = 0; Dias.size() > i; i++) {//Recorre los dias
3
  • le he agregado esta linea luego de cargar los días, lo cual me confirma que existe hasta la posición 4 pero sigue sin funcionar: for (int h = 0; h < Dias.size(); h++) { System.out.println("dia: " + Dias.get(h)+" indice: "+h); } el 27 may. 2018 a las 15:03
  • por favor coloca una captura del error y la linea q aparesca me la indiques el 27 may. 2018 a las 16:43
  • Dice que mi error esta en la linea 87: if (Objects.equals(Dias.get(i), Fechas_Pulpo.get(i))) { error: 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:87) el 27 may. 2018 a las 18:39

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.