How to change the value of array elements

前端 未结 7 2014
情歌与酒
情歌与酒 2020-12-21 10:57
int A = 300;
int B = 400;
int C = 1000;
int D = 500;

int []abcd = {A,B,C,D};
Arrays.sort(abcd);   // the sequence of array elements will be {300, 400, 500,1000}
         


        
7条回答
  •  旧时难觅i
    2020-12-21 11:22

    A naive way to do it would be to go through each element of the array, checking the values as you go:

    for (int i = 0; i < abcd.length; i++)
    {
        if (abcd[i] == A)
        {
            A = i+1;
        }
    }
    // Rinse and repeat for B, C, D
    

    If going down this approach, of course, turn it into a function that accepts the array, the value to search for, and returns its index within the array.

提交回复
热议问题