Skip to content

Instantly share code, notes, and snippets.

@fermopili
Created May 18, 2017 08:21
Show Gist options
  • Save fermopili/3022fe27547953fd52fe0f1684311a99 to your computer and use it in GitHub Desktop.
Save fermopili/3022fe27547953fd52fe0f1684311a99 to your computer and use it in GitHub Desktop.
com.javarush.task.task35.task3506
package com.javarush.task.task35.task3506;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
/*
Wildcards
*/
public class Solution
{
public static <D, H extends D, S extends H> void add1(List<D> destinationList, List<S> sourceList)
{
ListIterator<D> destListIterator = destinationList.listIterator ( );
ListIterator<S> srcListIterator = sourceList.listIterator ( );
for (int i = 0; i < sourceList.size ( ); i++)
{
destListIterator.add (srcListIterator.next ( ));
}
}
public static <T> void add(List<? super T> destinationList, List<? extends T> sourceList)
{
ListIterator<? super T> destListIterator = destinationList.listIterator ( );
ListIterator<? extends T> srcListIterator = sourceList.listIterator ( );
for (int i = 0; i < sourceList.size ( ); i++)
{
destListIterator.add (srcListIterator.next ( ));
}
}
public static void main(String[] args)
{
List<B> destination = new ArrayList<> ( );
destination.add (new B ( ));
List<C> source = new ArrayList<> ( );
source.add (new C ( ));
add (destination, source);
System.out.println (destination);
System.out.println (source);
/*
[com.javarush.test.level39.lesson08.task01.Solution$C@203b4f0e, com.javarush.test.level39.lesson08.task01.Solution$B@15c330aa]
[com.javarush.test.level39.lesson08.task01.Solution$C@203b4f0e]
*/
}
static class A
{
}
static class B extends A
{
}
static class C extends B
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment