public static<T> void append(Queue<T> ziel,Queue<T> quelle) {
Alles klar weiß für das nächste mal Bescheid.Code bitte nicht als Datei anfügen sondern über den Knopf </> direkt mit im Beitrag posten.
Dein Problem ist, dass Du in der Methode den Generic T einschränkst: T extends Iterable<T>
Integer ist aber nicht Iterable<T>.
Du hast als Parameter ja Queue<T> - das reicht doch. Da ist egal, was T ist. Es muss also nur das extends Iterable<T> raus, so dass Du:
public static<T> void append(Queue<T> ziel,Queue<T> quelle) {
hast.
public static <T> Collection<T> alternatingMerge(Collection<T> c1, Collection<T> c2) {
Collection<T> toRet = new LinkedList<>();
Queue<T> l1 = new LinkedList<>(c1);
Queue<T> l2 = new LinkedList<>(c2);
while (!l1.isEmpty() || !l2.isEmpty()) {
if (!l1.isEmpty() && !l2.isEmpty()) {
toRet.add(l1.remove());
toRet.add(l2.remove());
} else if (!l1.isEmpty()) {
toRet.add(l1.remove());
} else {
toRet.add(l2.remove());
}
}
return toRet;
}
public static void main(String[] args) {
List<String> l1 = List.of("eins", "zwei", "drei");
List<String> l2 = List.of("vier", "fünf", "sechs", "sieben", "acht");
List<String> l3 = (List<String>) alternatingMerge(l1, l2);
System.out.println("l1 = " + l1);
System.out.println("l2 = " + l2);
System.out.println("l3 = " + l3);
}
@coffeedrinker nein leider nicht, mein Problem wurde jedoch schon von dem @KonradN behoben. Trotzdem vielen Dank..@mrStudent Könnte es sein, dass eigentlich eher das/Collection gemeint war?:
Java:public static <T> Collection<T> alternatingMerge(Collection<T> c1, Collection<T> c2) { Collection<T> toRet = new LinkedList<>(); Queue<T> l1 = new LinkedList<>(c1); Queue<T> l2 = new LinkedList<>(c2); while (!l1.isEmpty() || !l2.isEmpty()) { if (!l1.isEmpty() && !l2.isEmpty()) { toRet.add(l1.remove()); toRet.add(l2.remove()); } else if (!l1.isEmpty()) { toRet.add(l1.remove()); } else { toRet.add(l2.remove()); } } return toRet; } public static void main(String[] args) { List<String> l1 = List.of("eins", "zwei", "drei"); List<String> l2 = List.of("vier", "fünf", "sechs", "sieben", "acht"); List<String> l3 = (List<String>) alternatingMerge(l1, l2); System.out.println("l1 = " + l1); System.out.println("l2 = " + l2); System.out.println("l3 = " + l3); }