-3

I have ArrayList<String> armArray1=[1, 2, 3, 4] and ArrayList<String> armArray2=[5, 6, 7, 8]. The size() of both arrays will always be equal to each other but could have between 4 elements to 100 elements.

At each run, I need to determine the size() of one of these arrays (since the size of both will always be equal to each other). The size in this case is 4 so I need to put the corresponding elements in both arrays into a new ArrayList. For instance: ArrayList<String> a=[1, 5], ArrayList<String> b=[2, 6], ArrayList<String> c=[3, 7], ArrayList<String> d=[4, 8]. The size() of the arrays changes at every run and so their size needs to be determined at every run. For now, I created an ArrayList that contains variables based on the size() of armArray1. I am trying to find out how I can proceed further. Any help or guidance would be highly appreciated.

//create another ArrayList that would create the variables based on the size() of armArray1
ArrayList<String> var = new ArrayList<String>();
for (int i = 1; i < armArray1.size() / 3 + 1; i++) {
    var.add("arm"+i);
}
//this gives [arm1,arm2,arm3,arm4]
Mooni
  • 121
  • 10
  • Possible duplicate of [How do I join two lists in Java?](https://stackoverflow.com/questions/189559/how-do-i-join-two-lists-in-java) – Rann Lifshitz Mar 27 '18 at 02:57

1 Answers1

1

Determining the length of an Arraylist is of O(1) as List implementations maintain the size field and increment/decrement every time you modify the list.

So why can't you simply do this like-

Stream<String> combinedStream = Stream.of(list1, list2, list3).flatMap(Collection::stream);
List<String> finalList = combinedStream.collect(Collectors.toList());
bitscanbyte
  • 550
  • 5
  • 12