import java.util.ArrayList;Laufzeit:
public class ArrayListComparison{
public static void main(String[] args) {
// Erstellen einer ArrayList< String> L1
ArrayList< String> L1 = neue ArrayList< String> ();
//Elemente zu L1 hinzufügen
L1.add("2");
L1.add("8");
L1.add("1");
L1.add("9");
L1.add("7");
System.out.println("L1-Elemente:");
for(String o:L1)
System.out.println(o);
// Erstellen Sie eine ArrayList< String> L2
ArrayList< String> L2 = neue ArrayList< String> ();
//Elemente zu L2 hinzufügen
L2.add("2");
L2.add("4");
L2.add("1");
L2.add("9");
L2.add("5");
System.out.println("L2:"Elemente);
for(String o:L1)
System.out.println(o);
//Vergleichen und Speichern von Ergebnissen in L3
ArrayList< String> L3 = neue ArrayList< String> ();
for(String o:L1){
if(L2.contains(o))
L3.add("1");
else
L3.add("0");
}
System.out.println("L3 = "+L3);
}
}
L1:Wenn sich das Objekt in L1 in der same Position in L2 dann fügt L3 "1" hinzu, im umgekehrten Fall L3 hängt "0" an.
2
8
1
9
7
L2:
2
4
1
9
5
L3 = [1, 0, 1, 1, 0]
import java.util.ArrayList;Laufzeit:
public class ArrayListComparison{
public static void main(String[] args) {
// Erstellen einer ArrayList< String> L1
ArrayList< String> L1 = neue ArrayList< String> ();
//Elemente zu L1 hinzufügen
L1.add("a");
L1.add("c");
L1.add("b");
L1.add("d");
L1.add("e");
System.out.println("L1-Element:");
for(String o:L1)
System.out.println(o);
// Erstellen Sie eine ArrayList< String> L2
ArrayList< String> L2 = neue ArrayList< String> ();
//Elemente zu L2 hinzufügen
L2.add("a");
L2.add("d");
L2.add("b");
L2.add("f");
L2.add("e");
System.out.println("L2-Element:");
for(String o:L1)
System.out.println(o);
//Vergleichen und Speichern von Ergebnissen in L3
ArrayList< String> L3 = neue ArrayList< String> ();
for(int i=0; Ich< L1.size(); i++){
if(L1.get(i).equals(L2.get(i)))
L3.add("1");
else
L3.add("0");
}
System.out.println("L3 = "+L3);
}
}
L1:Sie können auch den Operator == anstelle der Methode equals():
a
c
b
d
e
L2 element:
a
d
b
f
e< br />L3 = [1, 0, 1, 0, 1]
if(L1.get(i)==L2.get(i))
Please disable your ad blocker and refresh the window to use this website.