By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,469 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,469 IT Pros & Developers. It's quick & easy.

HELP!! My program cannot work for float array

P: 35
My program works alright for integer array, but not float array. How can I change the program so that I can get the original position of each element in the float array after sorting? Let's say my float array is {5.0f,10.0f,1.0f,15.0f}.
Here's my code:
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class SortArrays
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         Integer[] score = {5, 10, 0, 1, 15};
  8.  
  9.         ArrayList Values = new ArrayList();
  10.         HashMap OriginalPos = new HashMap();
  11.  
  12.         for(int i=score.length-1;i>=0;i--)
  13.         {
  14.             OriginalPos.put(new Integer(score[i]), new Integer(i));
  15.             Values.add(new Integer(score[i]));
  16.         }
  17.  
  18.         Collections.sort(Values);
  19.  
  20.         for(int i=Values.size()-1;i>=0;i--)
  21.         {
  22.             int val = ((Integer)(Values.get(i))).intValue();
  23.             System.out.print("" + val + "    ");
  24.             System.out.println(OriginalPos.get(new Integer(val)));
  25.         }
  26.     }
  27. }
Jan 30 '07 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
My program works alright for integer array, but not float array. How can I change the program so that I can get the original position of each element in the float array after sorting? Let's say my float array is {5.0f,10.0f,1.0f,15.0f}.
Here's my code:
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class SortArrays
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         Integer[] score = {5, 10, 0, 1, 15};
  8.  
  9.         ArrayList Values = new ArrayList();
  10.         HashMap OriginalPos = new HashMap();
  11.  
  12.         for(int i=score.length-1;i>=0;i--)
  13.         {
  14.             OriginalPos.put(new Integer(score[i]), new Integer(i));
  15.             Values.add(new Integer(score[i]));
  16.         }
  17.  
  18.         Collections.sort(Values);
  19.  
  20.         for(int i=Values.size()-1;i>=0;i--)
  21.         {
  22.             int val = ((Integer)(Values.get(i))).intValue();
  23.             System.out.print("" + val + " ");
  24.             System.out.println(OriginalPos.get(new Integer(val)));
  25.         }
  26.     }
  27. }
Remember 1.5 has autoboxing and unboxing. Also make use of generic types

Expand|Select|Wrap|Line Numbers
  1.  import java.util.*; 
  2. public class SortArrays {
  3.  public static void main(String args[]) {
  4.   Float[] score = {5.0f, 10.0f, 0.0f, 1.0f, 15.0f};
  5.   ArrayList<Float> values = new ArrayList<Float>();
  6.   HashMap<Float, Integer> originalPos = new HashMap<Float, Integer>();
  7.   for(int i=score.length-1;i>=0;i--) {
  8.    originalPos.put(score[i], i);
  9.    values.add(score[i]);
  10.   }
  11.   Collections.sort(values);
  12.   for(int i = values.size()-1; i >= 0; i--) {
  13.    float val = values.get(i);//No need for typecasting
  14.    System.out.print("" + val + "    ");
  15.    System.out.println(originalPos.get(val));
  16.   }
  17.  }
  18. }
  19.  
Jan 30 '07 #2

P: 35
Remember 1.5 has autoboxing and unboxing. Also make use of generic types

Expand|Select|Wrap|Line Numbers
  1.  import java.util.*; 
  2. public class SortArrays {
  3.  public static void main(String args[]) {
  4.   Float[] score = {5.0f, 10.0f, 0.0f, 1.0f, 15.0f};
  5.   ArrayList<Float> values = new ArrayList<Float>();
  6.   HashMap<Float, Integer> originalPos = new HashMap<Float, Integer>();
  7.   for(int i=score.length-1;i>=0;i--) {
  8.    originalPos.put(score[i], i);
  9.    values.add(score[i]);
  10.   }
  11.   Collections.sort(values);
  12.   for(int i = values.size()-1; i >= 0; i--) {
  13.    float val = values.get(i);//No need for typecasting
  14.    System.out.print("" + val + "    ");
  15.    System.out.println(originalPos.get(val));
  16.   }
  17.  }
  18. }
  19.  
Thanks for the solution, it works now.
Jan 30 '07 #3

10K+
P: 13,264
Thanks for the solution, it works now.
Anytime, huiling25. One day I shall ask what your user name means.
Jan 30 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.