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; } 
  • Well expanded capacity, and why addall? He does not need at all. - Sergey
  • one
    is the size of a and b assumed the same? - Russtam
  • @Sergey, if addAll () is removed, then at runtime we say IndexOutOfboundsException - alex_f
  • @Russtam, the same - alex_f
  • So no need to twist something from the tail, and then there will be no indexoutbounds then - Sergey

3 answers 3

Works for arrays of the same and different lengths:

 public static ArrayList function(ArrayList a, ArrayList b) { a.ensureCapacity(a.size() + b.size()); for (int i = 0; i < b.size(); i++) { int index = Math.min(i*2+1, a.size()); a.add(index, b.get(i)); } return a; } 

Variation of your example:

 public static ArrayList function2(ArrayList a, ArrayList b) { a.addAll(b); for (int i = b.size() - 1; i >= 0; i--) { int index = i*2 + 1; a.set(index, b.get(i)); a.set(index-1, a.get(i)); } return a; } 
  • The function does everything correctly, but the execution time is too long, for example, I have your solution running on a list of 100 thousand items in 2.3 seconds (I filled the lists with String objects with a length of 2 characters), my solution works in 0.02 seconds - alex_f
  • Thank you, your solution works a little faster and looks neater, the only thing I would do is declare the index variable before the loop) - alex_f
  • @alex_f why make out if outside the loop is not used? It does not affect the speed. - Russtam 1:58

ListIterator

 public static ArrayList stripingArray(ArrayList a, ArrayList b) { a.ensureCapacity(a.size() + b.size()); ListIterator it = a.listIterator(1); // располоТим курсор сразу Π·Π° ΠΏΠ΅Ρ€Π²Ρ‹ΠΌ элСмСнтом for (Object i : b) { it.add(i); if (it.hasNext()) it.next(); } return a; } 

Check what happened

 ArrayList<Integer> a = new ArrayList<Integer>() {{ add(1); add(3); add(5); add(7); }}; ArrayList<Integer> b = new ArrayList<Integer>() {{ add(2); add(4); add(6); add(8); }}; for (Object i : stripingArray(a, b)) { System.out.println(i); } 

It seems to be correct

 1 2 3 4 5 6 7 8 
  • Everything is correct, only it is performed again for a long time and plus you still use an additional object - alex_f
  • And why quickly, if each time you insert the tail of the array moves? Use LinkedList, then it will be fast. Probably. There is no movement in LinkedList-e. Structure specifically for quick modification of the middle of the list. - Sergey
  • Firstly, we need to effectively solve, and secondly, not to use additional objects - alex_f
  • LinkedList is the same extra object as ArrayList. Also in the standard set of classes. And it is intended precisely for the effective solution of such problems. - Sergey
  • But we get all the same ArrayList, so another collection will not be able to use - alex_f

Try this

 public ArrayList function(ArrayList a, ArrayList b){ //ИндСкс Π±ΡƒΠ΄ΡƒΡŽΡ‰Π΅Π³ΠΎ элСмСнта a int j=0; //ΠΏΠ΅Ρ€Π΅Π±ΠΈΡ€Π°Π΅ΠΌ b for(int i=0; i< b.size();i++){ //ВычисляСм индСкс элСмСнта j++; //добавляСм Π½Π° ΠΏΠΎΠ·ΠΈΡ†ΠΈΡŽ 1 a.add(j, b.get(i)); //Π‘Π΄Π²ΠΈΠ³Π°Π΅ΠΌ Π½Π° 1, Ρ‚.ΠΊ. индСкс измСнится j++; } return a; } 

In general, in fact, the calculation is as follows: insert 1 position 1 item from the list b . We obtain the shift of elements a by 1 (therefore, we increase the position index once more). Etc. that is, in fact we get that we need to insert at 1,3,5,7,9 ....