I want to learn how to join two lists, objects of type List, to generate a third list from the previous ones. I don't know how to do it more directly. I have developed a method that works and that I show below so that you understand what I intend, but I think it can be done in a more direct way using correctly the add or addAll methods of List, but I don't get it. In my method I first pass the List objects to an array, then I concatenate the arrays and the resulting array I pass to a List using Arrays.asList(array3).
private static List<Character> concatenarList(List<Character> listA, List<Character> listB ) {
Character[] array1 = new Character [listA.size()];
Character[] array2 = new Character [listB.size()];
listA.toArray(array1);//paso el objeto List list1 al array1
listB.toArray(array2);//paso el objeto list2 al array2
Character[] array3 = new Character[array1.length + array2.length];
for (int i = 0; i < array1.length; i++) {
array3[i] = array1[i];
}
int b = array1.length ;
for (int a = 0; a < array2.length; a++) {
array3[b] = array2[a];
b++;
}
List<Character> list3 = Arrays.asList(array3);
return list3;
}
You can use the List.addAll() method to concatenate two lists of the same object.
output