0
Hello im new to java can anyone help me with this task
Implement function that composes a new list of numbers consisting of elements of two passed lists arranged in ascending order, Implement as a function public static List cinteger > createNewList(List integer) list1, List<Integer> list2)
5 Antworten
+ 4
It works.
Output: 2, 3, 3, 5, 7, 8, 9
Just add
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
above the class / at the beginning of your code.
+ 1
Thanx I'll try it
0
Where do you need help?
0
public class Main {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<>(Arrays.asList(3, 9, 2, 7));
List<Integer> list2 = new ArrayList<>(Arrays.asList(8, 3, 5));
System.out.println(createNewList(list1, list2));
}
public static List<Integer> createNewList(List<Integer> list1, List<Integer> list2) {
List<Integer> newList = new ArrayList<>(list1.size()+list2.size());
newList.addAll(list1);
newList.addAll(list2);
newList.sort(Comparator.comparingInt((Integer a) -> a));
return newList;
}
}
0
This is my code is it right?