简体   繁体   中英

How can i move in string array item to the end of the array?

ipaddresses = IpAddresses.GenerateIps();

The array contain 56 items. I want item in index 2 to remove it to the end of the array. So the content in index 2 will be now in index 56. The array size will not change only the order of the items. Item 2 will be in place 56. So now i think index 3 will be index 2.

You can use System.arraycopy . This is similar to what happens when you remove an element from an ArrayList , only in your case you are moving the removed element to the end :

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;

Here elementData is the array and we move the element at the index position to the end.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM