tags:

views:

2337

answers:

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

+2  A: 

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.

Uri
Thanks Uri, I am actually do the array sorting so that it will be easier to located the biggest value of the Variable. Since variable C is the biggest value among other variable, C will change to 4 and its follow to the second biggest value D=3, B=2, A=1.
Jessy
Just put the values in an ArrayList, run Collections.sort on it, and pick the last one... thats your max.
John Ellinwood
Why not just use Collections.max() ?
Outlaw Programmer
I thought he wanted all of them in order, not just maximized.
Uri
+1  A: 

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];
Varkhan
I would add that this is true because int's are value types. Where they reference types, the variables and array elements would point to the same memory on the heap.
Ed Swangren
Yes, but sorting would still only shuffle the references in the array, and not change the variables...
Varkhan
Ahhh, I didn't pay close enough attention to the OP's question. No, that will not be the case.
Ed Swangren
+2  A: 

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.

Smashery
A: 

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.

MarkusQ
A: 

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.

endtime
A: 

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.

QuickRecipesOnSymbianOS
A: 

Would be nice to know what you exactly want to do.

This seams more like a quiz. My solution would be something like this:

Arrays.sort(abcd);
A = Arrays.binarySearch(abcd, A) + 1;
B = Arrays.binarySearch(abcd, B) + 1;
C = Arrays.binarySearch(abcd, C) + 1;
D = Arrays.binarySearch(abcd, D) + 1;

assuming time is not an issue and unique values...

OR,

if allowed, create a mutable Integer to use instead of the primitive int:

Int A = new Int(300);
Int B = new Int(400);
Int C = new Int(1000);
Int D = new Int(500);

Int []abcd = {A,B,C,D};
Arrays.sort(abcd);
for (int i = 0; i < abcd.length; i++)
    abcd[i].setInt(i+1);
Carlos Heuberger
would be nice to know why the -1 ...
Carlos Heuberger