机器人 - 扭转数组的顺序数组、机器人、顺序

2023-09-05 05:24:49 作者:27.习之孤往

我有对象的数组。

是否有可能使一个新的数组,它是这个数组的一个副本,但以相反的顺序? 我一直在寻找这样的事情。

  //我的数组
的ArrayList&其中;组件> mElements =新的ArrayList<组件>();
//新数组
的ArrayList&其中;组件> tempElements = mElements;

tempElements.reverse(); //东西扭转排列的顺序
 

解决方案

您可以分两步做到这一点:

 的ArrayList<组件> tempElements =新的ArrayList<组件>(mElements);
Collections.reverse(tempElements);
 
医生,我还能再抢救一下 但为你做手术的是AI智能机器人,害怕吗

I have an array of objects.

Is it possible to make a new array that is a copy of this array, but in reverse order? I was looking for something like this.

// my array
ArrayList<Element> mElements = new ArrayList<Element>();
// new array
ArrayList<Element> tempElements = mElements;

tempElements.reverse(); // something to reverse the order of the array

解决方案

You can do this in two steps:

ArrayList<Element> tempElements = new ArrayList<Element>(mElements);
Collections.reverse(tempElements);