JAVA How to get a third list based on two lists without a certain length?

0

In my code, I demonstrate how to start two lists with a number of elements, but what I occupy they are lists that are independent of the length of both. And that I compare the values and if they are equal they are passed to a new list, but if in my list A I do not have a value, that list B if it has, would be put on my list as "-value", that is, with a minus, and otherwise it would be "+ value".

    [![ArrayList<String> a=new ArrayList<String>(); 
     a.add("A");
     a.add("B");
     a.add("C");
     a.add(" ");
     a.add("E");
     a.add("F");
     a.add(" ");
     a.add(" ");
     a.add("I");
     a.add("J");
     a.add(" ");
     a.add(" ");
     a.add("M");
     a.add("N");

     ArrayList<String> b=new ArrayList<String>();
     b.add("A");
     b.add("B");
     b.add("C");
     b.add("D");
     b.add("E");
     b.add("F");
     b.add("G");
     b.add("H");
     b.add("I");
     b.add("J");
     b.add("K");
     b.add("L");
     b.add(" ");
     b.add("N");

     for(int i = 0; i< a.size(); i++){ //analiza los valores del arreglo a
     for(int j = 0; j< b.size(); j++){ //analiza los valores del arreglo b
            if((a.get(i).equals(b.get(j)) && b.get(i).equals(a.get(j))) && (b.get(j).equals(a.get(i)) && a.get(i).equals(b.get(j)))){ //Compara si los valores son iguales.
               System.out.println("\"" + a.get(i) + "\" ");

            }
            else {
              if(!(a.get(i).equals(b.get(j)) && b.get(i).equals(a.get(j))) && (b.get(j).equals(a.get(i)) && a.get(i).equals(b.get(j)))){


                   System.out.println("+" +b.get(i)+ "\"");  


                 System.out.println("-" +a.get(j) + "\"");  

              } 
              } 
                }


}][1]][1]

Outputs: "TO" "B" "C" + D " -M " "AND" "F" + G " -M " + H " -M " "I" "J" + K " -M " + L " -M " "N"

    
asked by Antonio Alejos 03.10.2018 в 18:13
source

1 answer

2

If you are using Java 7 you can use the contains method from ArrayList , as I do next in your code:

ArrayList<String> a=new ArrayList<String>(); 
 a.add("A");
 a.add("B");
 a.add("C");
 a.add(" ");
 a.add("E");
 a.add("F");
 a.add(" ");
 a.add(" ");
 a.add("I");
 a.add("J");
 a.add(" ");
 a.add(" ");
 a.add("M");
 a.add("N");

 ArrayList<String> b=new ArrayList<String>();
 b.add("A");
 b.add("B");
 b.add("C");
 b.add("D");
 b.add("E");
 b.add("F");
 b.add("G");
 b.add("H");
 b.add("I");
 b.add("J");
 b.add("K");
 b.add("L");
 b.add(" ");
 b.add("N");

 ArrayList<String> c=new ArrayList<String>();


 for (String valorA : a){
     if(!valorA.equals(" ")){
         if(b.contains(valorA)){
             c.add(valorA);
         }else{
             c.add("-"+valorA);
         }
     }
 }


 for (String valorB : b){
     if(!valorB.equals(" ")){
         if(!a.contains(valorB)){
             c.add("+"+valorB);
         }
     }
 }


 for (String valorC : c){
     System.out.print(valorC+" ");
 }

The first for what you do is compare the values in the to list with the b list, and those that are the same add them in the c list, otherwise add them but with a - . And in the second for just add to the c list with a + , the values in the b list it is not found in the to list. The third for only prints the values from the c list.

The result is the following: A B C E F I J -M N + D + G + H + K + L

If you exchange only the signs in the for's 1 and 2 the result would be the following:

A B C E F I J + M N -D -G -H -K -L

    
answered by 03.10.2018 / 19:08
source