英文:
Move a specific element in arraylist to the last index
问题
假设我有这个列表
List<Integer> grades = Arrays.asList(9, 8, 1, 0, 7, 0, 0, 3);
我想将所有的零移动到最后的索引位置。所以我希望输出类似于:
9, 8, 1, 7, 3, 0, 0, 0
我可以在简单数组中做到这一点,但是我仍然没有找出如何在ArrayList中实现这一点。
英文:
Let's say I have this List
List<Integer> grades = Arrays.asList(9,8,1,0,7,0,0,3);
I want to move all zeros to the last indexes .. So I want the output to be something like:
9,8,1,7,3,0,0,0
I can do that with simple arrays but I still didn't figure out how to do that in ArrayList.
答案1
得分: 3
你可以使用Comparator.comparing对列表进行相应排序,将所有的 0
移动到末尾,而不对其余的值进行排序。
grades.sort(Comparator.comparing(value -> value == 0 ? 1 /* 排在末尾 */ : 0 /* 不排序 */)); //[9, 8, 1, 7, 3, 0, 0, 0]
英文:
You can sort the list accordingly using Comparator.comparing, to move all 0
to last and not to sort the remaining values
grades.sort(Comparator.comparing(value -> value == 0 ? 1 /* sort last */ : 0 /* don't sort */)); //[9, 8, 1, 7, 3, 0, 0, 0]
答案2
得分: 2
正如评论中所指出的,您可以通过使用List
的get
和set
方法来重用您已经为数组编写的相同代码,而不是使用[]
运算符。
另一种“奇特”的方法可能是利用排序保持两个等效元素的相对位置的特性,并使用自定义的Comparator
对列表进行排序,该Comparator
根据项目是否等于0
进行排序 - 所有的零将被移到末尾,而所有其他项目将保持其相对位置:
grades.sort(Comparator.comparing(i -> i == 0));
英文:
As noted in the comments, you could reuse the same code you have for arrays by using the List
's get
and set
methods instead of the []
operator.
Another "sneaky" approach could be to utilize the fact that sorting keeps the relative positions of two elements that are equivalent, and sort the list with a custom Comparator
that sorts according to whether or not an item is equal to 0
- all the zeros will moved to the end, and all the other items will keep their relative postions:
grades.sort(Comparator.comparing(i -> i == 0));
答案3
得分: 1
对于比使用 O(n log(n)) 的 sort()
解决方案更快的 O(n) 实现,可以按照以下方式对数组进行操作:
int j = 0;
for (Integer value : grades) {
if (value != 0) {
grades.set(j++, value);
}
}
while (j < grades.size()) {
grades.set(j++, 0);
}
当然,如果不是 ArrayList
,使用 set(int, ?)
可能会影响性能,因此最好使用可更新的迭代器,即 ListIterator
:
ListIterator<Integer> iter = grades.listIterator();
for (Integer value : grades) {
if (value != 0) {
iter.next(); // 无需先调用 hasNext()
iter.set(value);
}
}
while (iter.hasNext()) {
iter.next();
iter.set(0);
}
作为对比,这是相应的数组解决方案:
int[] grades = {9,8,1,0,7,0,0,3};
int j = 0;
for (int i = 0; i < grades.length; i++) {
if (grades[i] != 0) {
grades[j++] = grades[i];
}
}
while (j < grades.length) {
grades[j++] = 0;
}
英文:
For a faster O(n) implementation than using an O(n log(n)) sort()
solution, do it the same way you'd do it for an array:
int j = 0;
for (Integer value : grades) {
if (value != 0) {
grades.set(j++, value);
}
}
while (j < grades.size()) {
grades.set(j++, 0);
}
Of course, using set(int, ?)
can be bad for performance if not an ArrayList
, so it'd be better to use an updatable iterator, i.e. a ListIterator
:
ListIterator<Integer> iter = grades.listIterator();
for (Integer value : grades) {
if (value != 0) {
iter.next(); // No need to call hasNext() first
iter.set(value);
}
}
while (iter.hasNext()) {
iter.next();
iter.set(0);
}
For comparison, here is the comparable array solution:
int[] grades = {9,8,1,0,7,0,0,3};
int j = 0;
for (int i = 0; i < grades.length; i++) {
if (grades[i] != 0) {
grades[j++] = grades[i];
}
}
while (j < grades.length) {
grades[j++] = 0;
}
答案4
得分: 0
感谢大家。。
我成功地用和处理简单数组相同的方式做到了:
List<Integer> price = Arrays.asList(9,8,1,0,7,0,0,3);
int count = 0;
for(int i=0; i<price.size(); i++) {
if(price.get(i) != 0) {
price.set(count++, price.get(i));
}
}
while(count < price.size()) {
price.set(count++, 0);
}
System.out.print(Arrays.toString(price.toArray()));
}
英文:
Thanks guys ..
I managed to do it the same way I did it with simple arrays :
List<Integer> price = Arrays.asList(9,8,1,0,7,0,0,3);
int count = 0;
for(int i=0; i<price.size(); i++) {
if(price.get(i) !=0) {
price.set(count++, price.get(i));
}
}
while(count<price.size()) {
price.set(count++, 0);
}
System.out.print(Arrays.toString(price.toArray()));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论