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"];

9
  • Wouldn't listC be null so you can't call .add()... and That for loop would give an IndexOutOfBounds Exception Commented Mar 17, 2016 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? Commented Mar 17, 2016 at 18:00
  • "that hold x amount of equal elements"--how exactly these elements are equal? Commented Mar 17, 2016 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 Commented Mar 17, 2016 at 18:10
  • you are right, I meant equal arraylist size @SashaSalauyou Commented Mar 17, 2016 at 18:12

4 Answers 4

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]
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for this response, the code worked perfectly, the app is now loading with no delay time. @PaulBoddington
@jpablo09 That's fantastic news. It was a long shot so I'm really glad it worked. AbstractList is my favourite Java class.
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.
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.
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;

2 Comments

This works only if listA and listB have the same size. new ArrayList(listA.size() + listB.size()); is useless.
The question has same size lists so I have suggested this. If both lists have different size then need to change this logic accordingly.
-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);

Comments

-3

You can try to do this.

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

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

and then sort

Collections.sort(listC);

6 Comments

This would append listB to the end of listA. What he wants is a merge where listA.get(N) is next to listB.get(N). Sorting wouldn't work if the lists aren't sorted already.
The sorting is at the end
@GiuseppeScopelliti There is not sorting since he wants them by index
@GiuseppeScopelliti What if list A was ["S", "B", "R", "K"]?
@GiuseppeScopelliti If list A was ["S", "B", "R", "K"] and list B was '["Z", "Q", "A", "R"] then the result should be ["S", "Z", "B", "Q", "R", "A", "K", "R"]. Sorting would put them in the improper order.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.