Java 如何将数组列表中的特定项移动到第一项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20186681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move specific item in array list to the first item
提问by user782104
For example : A list
例如:一个列表
A B C D E
ABCDE
Given C , Switch to
给定 C ,切换到
C A B D E
CADE
Notice that the array size will change, some items may removed in run times
请注意,数组大小会发生变化,某些项目可能会在运行时删除
Collections.swap(url, url.indexOf(itemToMove), 0);
This statement is not working because it output C B A D E not C A B D E , how to fix it?
此语句不起作用,因为它输出 CBADE 而不是 CABDE ,如何解决?
Thanks.
谢谢。
采纳答案by Chris Hayes
What you want is a very expensive operation in an ArrayList
. It requires shifting every element between the beginning of the list and the location of C
down by one.
你想要的是一个非常昂贵的ArrayList
. 它需要将列表开头和C
down位置之间的每个元素移动一个。
However, if you really want to do it:
但是,如果您真的想这样做:
int index = url.indexOf(itemToMove);
url.remove(index);
url.add(0, itemToMove);
If this is a frequent operation for you, and random access is rather less frequent, you might consider switching to another List
implementation such as LinkedList
. You should also consider whether a list is the right data structure at all if you're so concerned about the order of elements.
如果这对您来说是一个频繁的操作,而随机访问的频率较低,您可以考虑切换到另一个List
实现,例如LinkedList
. 如果您非常关心元素的顺序,您还应该考虑列表是否是正确的数据结构。
回答by Venkata Krishna
Do this:
做这个:
- Removethe element from the list:
ArraylistObj.remove(object);
- Addthe element backto the list at specific position:
ArrayListObj.add(position, Object);
- 从列表中删除元素:
ArraylistObj.remove(object);
- 将元素添加回特定位置的列表:
ArrayListObj.add(position, Object);
As per your code use this :
根据您的代码使用此:
url.remove("C");
url.add(0,"C");
回答by hbsrud
The problem is, you swap C with A, so A B C D E becomes C B A D E.
问题是,你把 C 换成 A,所以 ABCDE 变成了 CBAD E。
You could try something like this:
你可以尝试这样的事情:
url.remove(itemToMove);
url.add(0, itemToMove);
Or if url
is a LinkedList
:
或者如果url
是LinkedList
:
url.remove(itemToMove);
url.addFirst(itemToMove);
回答by sbc
This code will allow you to increase size of list, and insert elements without otherwise disturbing order of list
此代码将允许您增加列表的大小,并在不干扰列表顺序的情况下插入元素
private void insert(double price){
for(int i = 0; i < keys.size(); i++){
if(price > keys.get(i)){
keys.add(null);
for(int j = keys.size()-1; j > i; j--){
Collections.swap(keys, j, j-1);
}
keys.add(price);
Collections.swap(keys, keys.size()-1, i);
keys.remove(keys.size()-1);
return;
}
}
keys.add(price);
}
回答by Zulqarnain
Let say you have an array:
假设你有一个数组:
String[] arrayOne = new String[]{"A","B","C","D","E"};
Now you want to place the C
at index 0
get the C
in another variable
现在,你要放置C
在指数0
得到C
另一个变量
String characterC = arrayOne[2];
Now run the loop like following:
现在运行如下循环:
for (int i = (2 - 1); i >= 0; i--) {
arrayOne[i+1] = arrayOne[i];
}
Above 2
is index of C
. Now insert C
at index for example on 0
以上2
是 的索引C
。现在C
在索引处插入例如0
arrayOne[0] = characterC;
Result of above loop will be like that:
上面循环的结果将是这样的:
arrayOne: {"C","A","B","D","E"}
The end, we achieve our goal.
最终,我们实现了目标。
回答by MYLS
Another solution, just keep swaping from 0
to indexOf(itemToMove)
.
另一种解决方案,只需不断从0
to交换即可indexOf(itemToMove)
。
This is my Kotlin version:
这是我的 Kotlin 版本:
val list = mutableListOf('A', 'B', 'C', 'D', 'E')
(0..list.indexOf('C')).forEach {
Collections.swap(list, 0, it)
}
Sorry I am unfamiliar with Java but learned a little Kotlin. But the algorithm is the same.
抱歉,我不熟悉 Java,但学习了一点 Kotlin。但是算法是一样的。