Java 如何改变数组元素的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/703548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:32:53  来源:igfitidea点击:

How to change the value of array elements

javaarrays

提问by Jessy

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}

I wanted to change the value of the variables A,B,C,D according to their location in the array after sorting.

我想在排序后根据它们在数组中的位置更改变量 A、B、C、D 的值。

e.g variable A is located at index 0, so the value of A change to 1 instead of 300, variable B is located at index 1, so the value of B change to 2 instead of 400, variable D is located at index 2, so the value of D change to 3 instead of 500, variable C is located at index 3, so the value of C change to 4 instead of 1000,

例如变量 A 位于索引 0,因此 A 的值变为 1 而不是 300,变量 B 位于索引 1,因此 B 的值变为 2 而不是 400,变量 D 位于索引 2,所以D 的值从 500 变为 3,变量 C 位于索引 3,因此 C 的值变为 4 而不是 1000,

The final value of the variable will be: A = 1; B = 2; C = 4; D = 3;

变量的最终值将是:A = 1;乙 = 2; C = 4; D = 3;

回答by Uri

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.

我认为您对原语数组的工作方式有一些误解。创建数组 int[] abcd = {A,B,C,D} 时,它不包含变量 A、B、C 和 D,它应该包含它们值的副本。

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

因此,当您对它们进行排序时,实际上对 A、B、C 或 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.

如果您更清楚地说明您真正想做的事情,可能会更容易提供帮助。

回答by Varkhan

First of all, the variablesA, B, C and D have no "location in the array". What you did was to create a blank array with 4 slots, and affect the valuesof those variables in position 0, 1, 2 and 3.

首先,变量A、B、C 和 D 没有“在数组中的位置”。您所做的是创建一个带有 4 个插槽的空白数组,并影响位置 0、1、2 和 3 中这些变量的

When you sort the array, the values(once again) get shuffled between the array's slots, but sort() doesn't know anything about the variables A, B, C and D, so theirvalues remain unchanged. If you want those to change, you need to re-affect back the values into the variables, by using each slot's position:

当您对数组进行排序时,(再次)在数组的插槽之间进行混洗,但 sort() 对变量 A、B、C 和 D 一无所知,因此它们的值保持不变。如果您希望这些更改,您需要通过使用每个插槽的位置将值重新影响到变量中:

A = abcd[0];
B = abcd[1];
C = abcd[2];
D = abcd[3];

回答by Smashery

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.

如果采用这种方法,当然,将其转换为接受数组、要搜索的值并返回其在数组中的索引的函数。

回答by MarkusQ

Don't sort them. Put them in one array, have a second array initialized to all zeros, and count how many items are greater than or equal to each element. Then just transfer these counts back to the variables.

不要对它们进行排序。将它们放在一个数组中,将第二个数组初始化为全零,并计算有多少项大于或等于每个元素。然后只需将这些计数传输回变量。

回答by rofrankel

What you want is a mapping from value to array location. Unless there's a way to get such a mapping out of Arrays.sort(), which I doubt (though I'm no Java expert), you'll need to generate the mapping yourself. You could do this by searching the array. It might be more efficient to implement your own sort so that you can keep track of the mapping as you sort the array.

您想要的是从值到数组位置的映射。除非有办法从 Arrays.sort() 中获得这样的映射,我对此表示怀疑(尽管我不是 Java 专家),否则您需要自己生成映射。您可以通过搜索数组来做到这一点。实现您自己的排序可能更有效,以便您可以在对数组进行排序时跟踪映射。

回答by michael aubert

Are you looking for the syntax of array element access or something more complicated?

您是在寻找数组元素访问的语法还是更复杂的东西?

In Java,

在爪哇,

abcd[2] = 500; abcd[3] = 1000;

abcd[2] = 500;abcd[3] = 1000;

will make the modification you're looking for.

将进行您正在寻找的修改。

回答by Sonu Mishra

just loop it to change the value :

只需循环它即可更改值:

for(int i=0;i<abcd.length;i++){
      abcd[i] = i+1;
 }
System.out.println(Array.toString(abcd));