I verified it because I need to do the title to make a stacked bar graph. At first, I implemented it with a fixed class, but when I tried to use it for general purposes, I stumbled, so don't forget it.
java8
From a list of two Integers to one list of Integer arrays The two lists are assumed to be the same size.
Integer version
private List<Integer[]> convertTwoListToArray1(List<Integer> list1, List<Integer> list2) {
List<Integer[]> list = new ArrayList<>();
for (int i = 0; i < list1.size(); i++) {
list.add(new Integer[] { list1.get(i), list2.get(i) });
}
return list;
}
Click here for the generic version that I tried to generalize
Generics version
@SuppressWarnings("unchecked")
private <T> List<T[]> convertTwoListToArray2(List<T> list1, List<T> list2) {
List<T[]> list = new ArrayList<>();
for (int i = 0; i < list1.size(); i++) {
Object[] tmpArray = new Object[] { list1.get(i), list2.get(i) };
list.add((T[]) tmpArray);
}
return list;
}
I stumbled because I couldn't create an array of generics, but I solved it by casting an array of Objects.
that's all
Recommended Posts