如何将字符串数组项移动到数组的末尾?

时间:2015-08-05 10:08:51

标签: java arrays

ipaddresses = IpAddresses.GenerateIps();

该数组包含56个项目。我希望索引2中的项目将其删除到数组的末尾。 因此索引2中的内容现在将在索引56中。数组大小不会仅更改项目的顺序。第2项将到位56.所以现在我认为指数3将是指数2。

2 个答案:

答案 0 :(得分:4)

您可以使用System.arraycopy。这与从ArrayList中删除元素时发生的情况类似,只是在您将移除的元素移动到最后的情况下:

E element = elementData[index]; // get the element to be removed
int numMoved = elementData.length - index - 1;
// move all the elements that follow the moved element
if (numMoved > 0)
    System.arraycopy(elementData, index+1, elementData, index, numMoved);
// put the moved element at the end
elementData[elementData.length - 1] = element;

此处elementData是数组,我们将index位置的元素移动到结尾。

答案 1 :(得分:0)

试试这个:

String text = "Cool";    
text = text.replace(text.charAt(1), "") + text.charAt(1);