3

I have two array lists that hold x amount of equal elements, the idea is to combine both arraylists into one, but I want to add element by element in index order, currently I have this code listed below, but its not very efficient:

ArrayList <String> listA = ["a", "c", "e"]
ArrayList <String> listB = ["b", "d","f"]
ArrayList <String> listC;
for (int i = 0; i < listA.size() + listB.size(); i++){
    listC.add(listA.get(i));
    lictC.add(listB.get(i));
}
return listC;

output = ["a", "b", "c", "d", "e", "f"];

jpablo09
  • 35
  • 2
  • 6
  • Wouldn't `listC` be null so you can't call `.add()`... and That for loop would give an `IndexOutOfBounds Exception` – 3kings Mar 17 '16 at 18:00
  • 4
    The limit should be just `listA.size()`, not `listA.size() + listB.size()`. What do you mean, not very efficient? How else could you do it apart from reading all the elements and adding them one at a time? – Paul Boddington Mar 17 '16 at 18:00
  • *"that hold x amount of equal elements"*--how exactly these elements are equal? – Alex Salauyou Mar 17 '16 at 18:09
  • im implementing the code into an android application, so whenever I use listC to return the element I need in order to update a textview, the oncreate() is taking a long time start up the app, this is what I mean by not efficient. So performance wise, Im trying to find out if there's another way to this without using this method. @PaulBoddington – jpablo09 Mar 17 '16 at 18:10
  • you are right, I meant equal arraylist size @SashaSalauyou – jpablo09 Mar 17 '16 at 18:12
  • @jpablo09 then I can reference my answer to the similar question: http://stackoverflow.com/a/32720213/3459206 – Alex Salauyou Mar 17 '16 at 18:13
  • @jpablo09 In that case it sounds like an XY problem. http://meta.stackexchange.com/a/66378 Your real problem is how to improve the performance of starting up the app, not how to combine two `ArrayList`s. I'd try to create a minimal android example demonstrating the performance problem, because there's no way to drastically improve the performance of the problem you've shown us. – Paul Boddington Mar 17 '16 at 18:18
  • but in this case, the loop method is causing the delay of the start up. when I implemented a for each loop, the start up was instant; however, the elements in the list were out of order. I apologize upfront, im still in the process of learning. @PaulBoddington – jpablo09 Mar 17 '16 at 18:27
  • I propose the following https://stackoverflow.com/a/52012606/1568881 – Aguid Aug 24 '18 at 22:58

4 Answers4

5

As I indicate in the comments, I think this is an XY-problem.

However, this answer might help. It produces a List (but not an ArrayList) that is an alternating view of the two original Lists, avoiding the need to do any copying at all. As a result, it's O(1) rather than O(n).

public static <T> List<T> alternate(final List<? extends T> list1, final List<? extends T> list2) {
    final int size = list1.size();
    if (list2.size() != size)
        throw new IllegalArgumentException();
    return new AbstractList<T>() {
        @Override
        public int size() {
            return 2 * size;
        }
        @Override
        public T get(int i) {
            return ((i & 1) == 0 ? list1 : list2).get(i >> 1);
        }
    };
}

public static void main(String[] args) {
    List<String> list1 = Arrays.asList("A", "B", "C");
    List<String> list2 = Arrays.asList("D", "E", "F");
    System.out.println(alternate(list1, list2));  // prints [A, D, B, E, C, F]
}
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • thanks for this response, the code worked perfectly, the app is now loading with no delay time. @PaulBoddington – jpablo09 Mar 17 '16 at 22:27
  • @jpablo09 That's fantastic news. It was a long shot so I'm really glad it worked. `AbstractList` is my favourite Java class. – Paul Boddington Mar 17 '16 at 22:27
  • Will other List methods such as `iterator`, `indexOf` and `add` work? That's always the risk with inheritance, whereas composition doesn't have any of these issues. – Steve Kuo Mar 17 '16 at 23:01
  • Yes, they all work as they should. Methods that would mutate the list throw an exception. Usually you should favour composition over inheritance, but the point of `AbstractList` is that it is designed to be extended. – Paul Boddington Mar 19 '16 at 14:23
0

Just use one arraylist size in for loop. See my answer:

ArrayList <String> listA = ["a", "c", "e"]
ArrayList <String> listB = ["b", "d","f"]
ArrayList <String> listC = new ArrayList(listA.size() + listB.size());
for (int i = 0; i  < listA.size(); i++){
    listC.add(listA.get(i));
    lictC.add(listB.get(i));
}
return listC;
Brijesh Chopda
  • 195
  • 2
  • 11
  • This works only if listA and listB have the same size. new ArrayList(listA.size() + listB.size()); is useless. – Aguid Aug 24 '18 at 22:56
  • The question has same size lists so I have suggested this. If both lists have different size then need to change this logic accordingly. – Brijesh Chopda Aug 27 '18 at 10:31
-1

Could this code be useful for you?

ArrayList<ArrayList<String>> arraListOfArrayList = new ArrayList<ArrayList<String>>();
ArrayList<String> arrayList = new ArrayList<String>();
arraListOfArrayList.add(arrayList);
M. Mariscal
  • 1,226
  • 3
  • 17
  • 46
-3

You can try to do this.

ArrayList<String> listC = new ArrayList<>();

listC.addAll(listA);
listC.addAll(listB);

and then sort

Collections.sort(listC);