I have a task where I need to write a method which allows me to mix two generic lists and return a new mixed list.
The mixed list should have the first element of l1 at the first position of the new mixed list, the first element of the l2 should be in second position and then the second element of l1 should be in third position and so on.
If one list is longer than the other the rest should just be added in original order.
An example is: l1 = (1,2,3) and l2=(9,8) --> mixed list = (1,9,2,8,3)
public <S, T> List<T> listeMischen(List<S> l1, List<T> l2) { List<T> newlist = new ArrayList<T>(); for(int i = 0; i < l1.size(); i++) { for(int j = 0; j < l2.size(); j++) { newlist.add(charAt(i)); newlist.add(charAt(j)); } } return newlist; } P.S. I do not know how to properly add the elements as they are generic. I have typed in the absolutely wrong method of "charAt", just to showcase what I would attempt to do if the type wasn't generic but a character. Since the elements however can be generic I am very unsure of what to do.