java语言alist,如何在Java中反轉列表?

I want to have a reversed list view on a list (in a similar way than List#sublist provides a sublist view on a list). Is there some function which provides this functionality?

我希望列表上有一個反向的列表視圖(與列表#子列表在列表上提供子列表視圖的方式類似)。有什么功能可以提供這個功能嗎?

I don't want to make any sort of copy of the list nor modify the list.

我不想復制或修改列表。

It would be enough if I could get at least a reverse iterator on a list in this case though.

在這種情況下,如果我能在列表中至少得到一個反向迭代器就足夠了。

Also, I know how to implement this myself. I'm just asking if Java already provides something like this.

而且,我自己也知道如何實現它。我只是問,Java是否已經提供了這樣的東西。

Demo implementation:

演示實現:

static Iterable iterableReverseList(final List l) {

return new Iterable() {

public Iterator iterator() {

return new Iterator() {

ListIterator listIter = l.listIterator(l.size());

public boolean hasNext() { return listIter.hasPrevious(); }

public T next() { return listIter.previous(); }

public void remove() { listIter.remove(); }

};

}

};

}

I just have found out that some List implementations have descendingIterator() which is what I need. Though there is no general such implementation for List. Which is kind of strange because the implementation I have seen in LinkedList is general enough to work with any List.

我剛剛發現有些列表實現有我需要的后代迭代器()。雖然列表沒有通用的實現。這有點奇怪,因為我在LinkedList中看到的實現足以處理任何列表。

11 个解决方案

#1

166

番石榴提供:Lists.reverse(列表)

List letters = ImmutableList.of("a", "b", "c");

List reverseView = Lists.reverse(letters);

System.out.println(reverseView); // [c, b, a]

Unlike Collections.reverse, this is purely a view... it doesn't alter the ordering of elements in the original list. Additionally, with an original list that is modifiable, changes to both the original list and the view are reflected in the other.

與集合。相反,這純粹是一種觀點……它不會改變原始列表中元素的順序。此外,對於可修改的原始列表,對原始列表和視圖的更改都反映在另一個列表中。

#2

178

Use the .clone() method on your List. It will return a shallow copy, meaning that it will contain pointers to the same objects, so you won't have to copy the list. Then just use Collections.

使用列表中的.clone()方法。它將返回一個淺拷貝,這意味着它將包含指向相同對象的指針,因此您不必復制列表。然后使用集合。

Ergo,

因此,

Collections.reverse(list.clone());

If you are using a List and don't have access to clone() you can use subList():

如果您正在使用一個列表,並且無法訪問clone(),那么您可以使用subList():

List> shallowCopy = list.subList(0, list.size());

Collections.reverse(shallowCopy);

#3

71

If i have understood correct then it is one line of code .It worked for me .

如果我理解正確的話,這就是一行代碼,它對我很有用。

Collections.reverse(yourList);

#4

23

Its not exactly elegant, but if you use List.listIterator(int index) you can get a bi-directional ListIterator to the end of the list:

它不是很優雅,但如果你使用列表的話。listIterator(int index)你可以在列表的最后找到一個雙向listIterator:

//Assume List foo;

ListIterator li = foo.listIterator(foo.size());

while (li.hasPrevious()) {

String curr = li.previous()

}

#5

10

Collections.reverse(nums) ... It actually reverse the order of the elements. Below code should be much appreciated -

Collections.reverse(num)…它實際上顛倒了元素的順序。下面的代碼應該非常感謝-

List nums = new ArrayList();

nums.add(61);

nums.add(42);

nums.add(83);

nums.add(94);

nums.add(15);

//Tosort the collections uncomment the below line

//Collections.sort(nums);

Collections.reverse(nums);

System.out.println(nums);

Output: 15,94,83,42,61

輸出:15,94、83、61

#6

4

java.util.Deque has descendingIterator() - if your List is a Deque, you can use that.

java.util。Deque有后代迭代器()——如果你的列表是Deque,你可以使用它。

#7

3

I know this is an old post but today I was looking for something like this. In the end I wrote the code myself:

我知道這是一個舊的帖子,但今天我在找這樣的東西。最后,我自己寫了代碼:

private List reverseList(List myList) {

List invertedList = new ArrayList();

for (int i = myList.size() - 1; i >= 0; i--) {

invertedList.add(myList.get(i));

}

return invertedList;

}

Not recommended for long Lists, this is not optimized at all. It's kind of an easy solution for controlled scenarios (the Lists I handle have no more than 100 elements).

不推薦長列表,這根本沒有優化。對於受控場景來說,這是一種簡單的解決方案(我處理的列表不超過100個元素)。

Hope it helps somebody.

希望它能幫助別人。

#8

2

I use this:

我用這個:

public class ReversedView extends AbstractList{

public static List of(List list) {

return new ReversedView<>(list);

}

private final List backingList;

private ReversedView(List backingList){

this.backingList = backingList;

}

@Override

public E get(int i) {

return backingList.get(backingList.size()-i-1);

}

@Override

public int size() {

return backingList.size();

}

}

like this:

是這樣的:

ReversedView.of(backingList) // is a fully-fledged generic (but read-only) list

#9

1

You can also do this:

你也可以這樣做:

static ArrayList reverseReturn(ArrayList alist)

{

if(alist==null || alist.isEmpty())

{

return null;

}

ArrayList rlist = new ArrayList<>(alist);

Collections.reverse(rlist);

return rlist;

}

#10

0

You can also invert the position when you request an object:

您還可以在請求對象時反轉該位置:

Object obj = list.get(list.size() - 1 - position);

#11

0

For small sized list we can create LinkedList and then can make use of descending iterator as:

對於較小的列表,我們可以創建LinkedList,然后使用降序迭代器:

List stringList = new ArrayList<>(Arrays.asList("One", "Two", "Three"));

stringList.stream().collect(Collectors.toCollection(LinkedList::new))

.descendingIterator().

forEachRemaining(System.out::println); // Four, Three, Two, One

System.out.println(stringList); // One, Two, Three, Four

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值