473,499 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HELP!! My program cannot work for float array

35 New Member
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
3 3068
r035198x
13,262 MVP
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
huiling25
35 New Member
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
r035198x
13,262 MVP
Thanks for the solution, it works now.
Anytime, huiling25. One day I shall ask what your user name means.
Jan 30 '07 #4

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

Similar topics

1
4368
by: Giovanni Azua | last post by:
Hello all, I need to return back to TSQL two numeric values from an Extended Stored Procedure developed in C. As result my C dll program produces two float values, the TSQL side expects to have...
3
2885
by: dbru | last post by:
I need to pass an address of a Managed float array to a DLL. The following doesn't seem to work extern static float GetXXX( StringBuilder HWND, long nWhat, ref float lparam );
4
1606
by: Charts | last post by:
6/23/05 ..NET Development\Framework\dotnet.framework.aspnet Visual Studio 2005 Beta 2 project cannot work in IIS virtual directory I used Visual Studio 2005 Beta 2 to build a simple new...
4
6056
by: viks | last post by:
Hi guys I need little help here . I want to convert 'System::Object __gc * array ' to 'float array' Lets say I have object Reader with method Send ,it returns a variant that contains a...
4
25012
by: Pixie Songbook | last post by:
Hi, I'm trying to write a program using the Dev C++ 4.9.9.2 compiler that takes input numbers from a word document, sums them together, and then gives me a result. It should be easy as the book I...
13
27389
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
2
2003
by: Peter | last post by:
I have OLE Object field in Access Database. This field contains an array of floats. The array was moved from memory into a string and the string was saved as OLE Object in a database (That was...
8
1429
by: peter gray | last post by:
I would like the following code to do a calculation based on the results of a for(var i...etc loop. I cannot 'feed ' the array results into the calculation and keep on getting NaN. Can someone help...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.