How to change the value of array elements

前端 未结 7 2010
情歌与酒
情歌与酒 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条回答
  • 2020-12-21 11:39

    I think you are misunderstanding something about the way that arrays of primitives work. When you create the array int[] abcd = {A,B,C,D}, it does not contain the variables A, B, C, and D, it should contain copies of their values.

    Hence, when you sort them, you do not actually have any impact on A, B, C, or D.

    One way to accomplish something close to what you are trying to do would be to use a sorted map, where each value would map into a value holder, you can then iterate over the keys (in a sorted order) and assign a sequential number to each value holder.

    If you clarify more about what you're really trying to do, it may be easier to help.

    0 讨论(0)
提交回复
热议问题