The method accepts 2 ArrayList: a, b. As a result, he must return a link to the first sheet, which will contain alternating elements of the 1st and 2nd list, i.e. (a0, b0, a1, b1 ..., an, bn). The second list can not be changed. At first, I solved this problem using an additional object, creating another list.
public ArrayList function(ArrayList a, ArrayList b) { ArrayList c = new ArrayList(a.size()+b.size()); for ( int i = 0 ; i < a.size() ; i++ ) { c.add(a.get(i)); c.add(b.get(i)); } a.clear(); a.addAll(c); return a; } But you need to solve the problem without using an additional object. At the beginning, I simply thought of expanding the capacity of list a, and then starting from the end, alternately add elements from list b and a. But the method ensureCapacity () does not recreate an array of the necessary capacity, but only increases the capacity of the list. As a result, I do not know what to do with the line of the form: a.addAll (a). You just have to add the elements first, and then replace them.
public ArrayList function(ArrayList a, ArrayList b) { a.ensureCapacity(a.size() + b.size()); a.addAll(a); for ( int i = (b.size()-1), j = (a.size() - 1) ; i > -1; i--, j-- ) { a.set(j--, b.get(i)); a.set(j, a.get(i)); } return a; }