Java: moving items in array

前端 未结 6 1673
囚心锁ツ
囚心锁ツ 2020-12-11 12:31

I\'m looking to move things around within an Array.

I want to be able to move the last item within the given Array to a spot while moving those in the current locati

6条回答
  •  佛祖请我去吃肉
    2020-12-11 13:15

    First of all, in your code you do

    for (int x = stuff.length-1; x > pos; x--)  
    

    where pos is not even defined, I suggest on changing it to position. second of all, change the "list" to "stuff".

    Modified, working code:

    public static void moveLastup(String[] stuff, int position) 
        {
            String y = stuff[stuff.length-1];
    
            for (int x = stuff.length-1; x > position; x--)  
                stuff[x] = stuff[x-1];
    
            stuff[position] = y;
        }
    

提交回复
热议问题