我有对象的数组。
是否有可能使一个新的数组,它是这个数组的一个副本,但以相反的顺序? 我一直在寻找这样的事情。
//我的数组
的ArrayList&其中;组件> mElements =新的ArrayList<组件>();
//新数组
的ArrayList&其中;组件> tempElements = mElements;
tempElements.reverse(); //东西扭转排列的顺序
解决方案
您可以分两步做到这一点:
的ArrayList<组件> tempElements =新的ArrayList<组件>(mElements);
Collections.reverse(tempElements);
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);