0

I have a problem with arrays in java. I have a array:

double[][] tab2 = {{318, 0.0825},
                  {321, 0.1131},
                  {309, 0.0283},
                  {319, 0.0830}};

and I need to change that array to:

double[][] tab2 = {{318, 0.0830},
                  {321, 0.0283},
                  {309, 0.1131},
                  {319, 0.0825}};

This is what i mean. I have four numbers. In this example 318, 321, 309 and 319. Every number is connected with another number, like 318 - 0.0825, 321 - 0.1131, etc. I need change value second values. When number which is connected with the biggest number should be connected with the smaller eg. 321 - 0.1131 should be 321 - 0.0283, 318 - 0.00830 should be 318 - 0.0825. It is possible to do this things?

4
  • 1
    Map<Long, Double> would be better choice in this case. Commented Dec 10, 2011 at 13:44
  • A map would be a better choice; still leaves the problem of sorting though. Commented Dec 10, 2011 at 13:47
  • Simple matter of programming. Sort the two lists separately and then update the original based on the sort results. Ideally you'd sort the first column using an index/tag sort, so you'd directly get the index values, but for a small list it's not impractical to go back and search your original list for matches. Commented Dec 10, 2011 at 13:51
  • (Or, if the original order is not important, you could just take the two sorted lists and re-join them, taking items from the second in reverse order.) Commented Dec 10, 2011 at 13:52

4 Answers 4

4

It would be easier to do this if the arrays were arranged in transposed form.

double[][] tab3 = {
    {318.0, 321.0, 309.0, 319.0},
    {0.0830, 0.1131, 0.0283, 0.0830}}

You can then sort the first array (tab3[0]) in ascending order and then the second array (tab3[1]) in descending order. Then all of the array indexes will line up and match the largest to the smallest. double[0][0] will be the biggest and double[1][0] will be the smallest, matching 309 and 0.1131 together.

double[][] tab3 = {
    {309.0, 318.0, 319.0, 321.0},
    {0.1131, 0.0830, 0.0830, 0.0283}}
Sign up to request clarification or add additional context in comments.

2 Comments

This is good, but not for me. Those first numbers could be different. Number 309 could be 328, so your solution is not for me, but thx.
That's why you sort both arrays, as I explained. If you need them to be in that exact order, you could save the original positions of each one before sorting and reconstruct them later.
1

You can use Map in this case, Specify first number as key and second number as value. Refer below document for more details about Map.

http://docs.oracle.com/javase/7/docs/api/java/util/Map.html

1 Comment

Your java doc link is too old(1.4.2). Update it to java 7.
0

This will probably not be the most efficient solution, but it will work

double[][] tab2 = {{318, 0.0825},
              {321, 0.1131},
              {309, 0.0283},
              {319, 0.0830}};
List<Double> firstEntries = new ArrayList<Double>(  );
List<Double> secondEntries = new ArrayList<Double>(  );
for ( double[] doubles : tab2 ) {
  firstEntries.add( doubles[ 0 ] );
  secondEntries.add( doubles[ 1 ] );
}
Collections.sort( firstEntries );
Collections.sort( secondEntries );
Collections.reverse( secondEntries );
//now the lists contain the entries in the correct order
//if needed, they can be grouped again in an array
for ( int i = 0; i < tab2.length; i++ ) {
  double[] doubles = tab2[ i ];
  doubles[1] = secondEntries.get( firstEntries.indexOf( doubles[0] ) );
}

Comments

0

You can achieve this by:

HashMap<Integer,Double> mapValues = new HashMap<Integer,Double>();
// Insert keys and values into mapValues
Integer[] keys  = mapValues.KeySet().toArray(new Integer[]{});
Double[] values = mapValues.Values().toArray(new Double[]{});
Arrays.sort(keys,Collections.reverseOrder());
Arrays.sort(values);
HashMap<Integer,Double> resultMap = new HashMap<Integer,Double>();
for(int i=0; i<keys.length; i++){
   resultMap.put(keys[i], values[i]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.