Jessy
Jessy

Reputation: 15661

How to change the value of array elements

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.

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,

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

Upvotes: 2

Views: 92662

Answers (7)

Sonu Mishra
Sonu Mishra

Reputation: 25

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));

Upvotes: 0

michael aubert
michael aubert

Reputation: 6826

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

In Java,

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

will make the modification you're looking for.

Upvotes: 2

rofrankel
rofrankel

Reputation: 2486

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.

Upvotes: 0

MarkusQ
MarkusQ

Reputation: 21950

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.

Upvotes: 0

Smashery
Smashery

Reputation: 59723

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.

Upvotes: 6

Varkhan
Varkhan

Reputation: 16761

First of all, the variables A, 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 values of those variables in position 0, 1, 2 and 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 their values 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:

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

Upvotes: 2

Uri
Uri

Reputation: 89859

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.

Upvotes: 5

Related Questions