Show part of the contents of a list in java

0

I am trying to show part of the content of a list that I have but I do not know how to do it. I have a list of facilities with 30 selection options but I want to see only 3, that is, I have this list for example: Fuel station Wheels Seating Boxes Changes Frills Motors Moons Screws ... And I want only changes, motors and moons to appear to me

    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());
            }
    
asked by inauski 15.11.2018 в 09:04
source

1 answer

0

You can use a break and a control variable to know when to stop tracing the 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) + "]";
    
answered by 15.11.2018 в 10:11