Go through 2 lists in java

2
    List<String> DiasSemana = new ArrayList<>();
    DiasSemana.add("Lunes");
    DiasSemana.add("Martes");
    DiasSemana.add("Miércoles");
    DiasSemana.add("Jueves");
    DiasSemana.add("Viernes");
    DiasSemana.add("Sábado");
    DiasSemana.add("Domingo");

    List<String> DiasTRabajo = new ArrayList<>();
    DiasTRabajo.add("Lunes");
    DiasTRabajo.add("Miércoles");
    DiasTRabajo.add("Viernes");
    DiasTRabajo.add("Domingo");

    for (String ds : DiasSemana) {
        for (String dt : DiasTRabajo) {
            if (ds.equals(dt)) {
                System.out.println("Trabaja : " + ds);
                break;
            } else {
                System.out.println("NO Trabaja : " + ds);
            }
        }
    }

I've been trying for a while and I do not get a result. I want that from the lists I get as a result

Trabaja : Lunes
No Trabaja : Martes
Trabaja : Miércoles
No Trabaja : Jueves
Trabaja : Viernes
No Trabaja : Sábado
Trabaja : Domingo

It occurred to me to do so. If someone can correct me or enlighten me with a better way I will thank you very much.

    
asked by Carlos Rivas 20.04.2018 в 04:35
source

4 answers

2

The problem you're having is that for each item in the list DiasSemana you're going through the entire list DiasTRabajo and for each element of the latter print something, whether it worked or not. This is not correct, because in order to determine if in the list DiasTRabajo is the day analyzed according to the list DiasSemana and print whether it worked or not, you must go through it until you find that day, or not, and at that moment, When you finish going through it, you must indicate whether it worked or not. For example:

    boolean found;
    for (String ds : DiasSemana) {

        found = false;
        for (String dt : DiasTRabajo) {
            if (ds.equals(dt)) {
                found = true;
                break;
            }
        }

        if (found) {
            System.out.println("Trabaja : " + ds);
        } else {
            System.out.println("NO Trabaja : " + ds);
        }
    }

If you notice, the variable found used it to put it as true if I find the analyzed day. If when leaving the for nested (the inside) found is true, then indicated that it worked and otherwise indicated that it was not worked and proceed to analyze the next day of the week.

The break I use to finish the execution of the for nested if I find the day.

    
answered by 20.04.2018 / 05:08
source
3

Guys respect its solution but it is not necessary to go through the two lists with just one stroke, since the ArrayList have the constains method which returns a Boolean value if one of the elements that is passed is in the list to be verified , here my solution:

See Java.Util.ArrayList.contains documentation

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
        ArrayList<String> DiasSemana = new ArrayList<>();
    DiasSemana.add("Lunes");
    DiasSemana.add("Martes");
    DiasSemana.add("Miércoles");
    DiasSemana.add("Jueves");
    DiasSemana.add("Viernes");
    DiasSemana.add("Sábado");
    DiasSemana.add("Domingo");

    ArrayList<String> DiasTRabajo = new ArrayList<>();
    DiasTRabajo.add("Lunes");
    DiasTRabajo.add("Miércoles");
    DiasTRabajo.add("Viernes");
    DiasTRabajo.add("Domingo");

        for (String ds : DiasSemana) {
             System.out.println(DiasTRabajo.contains(ds) ? "Trabaja : " + ds :"NO Trabaja : " + ds);
        }
    }
}
    
answered by 20.04.2018 в 06:38
0

Better with Java8, the code is clearer:

diasSemana.stream().forEach( (d) -> { String laborable = diasTRabajo.contains(d) ? " Trabaja" : " Libra"; System.out.println(d + laborable); });

    
answered by 20.04.2018 в 06:54
0

I did it in the following way with your example:

import java.util.ArrayList;
import java.util.List;

public class MyClass {
public static void main(String args[]) {

    String Dia = "";
    boolean check = false;

    List<String> DiasSemana = new ArrayList<>();
    DiasSemana.add("Lunes");
    DiasSemana.add("Martes");
    DiasSemana.add("Miércoles");
    DiasSemana.add("Jueves");
    DiasSemana.add("Viernes");
    DiasSemana.add("Sábado");
    DiasSemana.add("Domingo");

    List<String> DiasTRabajo = new ArrayList<>();
    DiasTRabajo.add("Lunes");
    DiasTRabajo.add("Miércoles");
    DiasTRabajo.add("Viernes");
    DiasTRabajo.add("Domingo");

        for (String ds : DiasSemana) {
            check = false;
            for (String dt : DiasTRabajo) {
                    if (ds.equals(dt)) {
                        check = true;
                        Dia = ds;
                    } else {
                        Dia = ds;
                    }
            }
            if(check)
            {
               System.out.println("Trabaja : " + Dia); 
            }
            else{
                System.out.println("NO Trabaja : " + Dia);
            }


        }
}
}

Ready.

    
answered by 20.04.2018 в 05:53