如何更改数组列表的大小以匹配JAVA中给定数组列表的大小
我有两个不同大小的数组列表,所以我想让较大的数组列表等于较小的数组列表如何更改数组列表的大小以匹配JAVA中给定数组列表的大小,java,arrays,Java,Arrays,我有两个不同大小的数组列表,所以我想让较大的数组列表等于较小的数组列表 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));
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));
如果可以将列表声明为
列表
而不是数组列表
List<Double> aListX
这就是方法:)
aListX=newarraylist(aListX.subList(0,aListY.size());
您可以使用迭代器删除第5个索引后的元素
Iterator iterate = aListX.iterator();
int index = 0;
while (iterate.hasNext()) {
iterate.next();
if (index >= aListY.size()) {
iterate.remove();
}
++index;
}
应该删除什么?看起来你需要一个更大的列表中删除什么值重要吗?还是要将值分布到两个列表中?@JoakimDanielson在本例中保留前五个元素ArrayList aListX=new ArrayList(Arrays.asList(x).subList(0,y.length));
aListX = aListX.subList(0, aListY.size());
aListX = new ArrayList<>(aListX.subList(0, aListY.size()));
Iterator iterate = aListX.iterator();
int index = 0;
while (iterate.hasNext()) {
iterate.next();
if (index >= aListY.size()) {
iterate.remove();
}
++index;
}