I have two array lists which in different sizes, so I want to make the array list which is larger in size equal to smaller one size.
Double[] x = {
14.2, 16.4, 11.9, 15.2, 18.5, 22.1, 19.4, 25.1, 23.4, 18.1, 22.6, 17.2
};
Double[] y = {
17.5, 14.2, 12.2, 16.0, 19.3
};
ArrayList<Double> aListX = new ArrayList<>(Arrays.asList(x));
ArrayList<Double> aListY = new ArrayList<>(Arrays.asList(y));
I want ArrayList aListX
as the same size as an ArrayList aListY
Result:
aListX.size() = 5
答案 0 :(得分:0)
If you can declare the list as a List
instead of an ArrayList
List<Double> aListX
then you can do
aListX = aListX.subList(0, aListY.size());
答案 1 :(得分:0)
This is the way to do it :)
aListX = new ArrayList<>(aListX.subList(0, aListY.size()));
答案 2 :(得分:0)
您可以使用迭代器删除第5个索引之后的元素
Iterator iterate = aListX.iterator();
int index = 0;
while (iterate.hasNext()) {
iterate.next();
if (index >= aListY.size()) {
iterate.remove();
}
++index;
}