0

estoy intentando mostrar parte del contenido de una lista que tengo pero no se como hacerlo. Tengo una lista de instalaciones con 30 opciones de selección pero quiero que me aparezcan solo 3, es decir, tengo por ejemplo esta lista: Gasolinera Ruedas Asientos Cajas Cambios Volantes Motores Lunas Tornillos ... Y quiero que solo me aparezcan Cambios, motores y lunas

    try {
                if (this.getInstalacion()!=null && this.getInstalacion().length>0) {
                    selectedInst = "[";
                    for (Integer id : this.getInstalacion()) {
                        selectedInst += this.getInstallationh().get(id.toString()) + ";";
                    }
                   selectedInst = selectedInst.substring(0, selectedInst.length()-1) + "]";
                if (this.getZona()!=null && this.getZona().length>0) {
                        selectedInst += " (...)";
                    }
        }
            } catch (Exception e) {
                logger.warning("ajaxSelectedInstallations (getting installations): " + e.toString());
            }

1 respuesta 1

0

Puedes usar un break y una variable de control para saber cuando parar de recorrer el for.

if (this.getInstalacion()!=null && this.getInstalacion().length>0) {
    selectedInst = "[";
    int contador = 0; // Inicializas el contador a 0
    for (Integer id : this.getInstalacion()) {
        selectedInst += this.getInstallationh().get(id.toString()) + ";";
        contador++; // Aumentas en uno el contador
        if(contador == 3){
            break; // Rompemos el for
        }
    }
    selectedInst = selectedInst.substring(0, selectedInst.length()-1) + "]";

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.