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: - import java.util.*;
-
-
public class SortArrays
-
{
-
public static void main(String args[])
-
{
-
Integer[] score = {5, 10, 0, 1, 15};
-
-
ArrayList Values = new ArrayList();
-
HashMap OriginalPos = new HashMap();
-
-
for(int i=score.length-1;i>=0;i--)
-
{
-
OriginalPos.put(new Integer(score[i]), new Integer(i));
-
Values.add(new Integer(score[i]));
-
}
-
-
Collections.sort(Values);
-
-
for(int i=Values.size()-1;i>=0;i--)
-
{
-
int val = ((Integer)(Values.get(i))).intValue();
-
System.out.print("" + val + " ");
-
System.out.println(OriginalPos.get(new Integer(val)));
-
}
-
}
-
}
3 3068
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: - import java.util.*;
-
-
public class SortArrays
-
{
-
public static void main(String args[])
-
{
-
Integer[] score = {5, 10, 0, 1, 15};
-
-
ArrayList Values = new ArrayList();
-
HashMap OriginalPos = new HashMap();
-
-
for(int i=score.length-1;i>=0;i--)
-
{
-
OriginalPos.put(new Integer(score[i]), new Integer(i));
-
Values.add(new Integer(score[i]));
-
}
-
-
Collections.sort(Values);
-
-
for(int i=Values.size()-1;i>=0;i--)
-
{
-
int val = ((Integer)(Values.get(i))).intValue();
-
System.out.print("" + val + " ");
-
System.out.println(OriginalPos.get(new Integer(val)));
-
}
-
}
-
}
Remember 1.5 has autoboxing and unboxing. Also make use of generic types - import java.util.*;
-
public class SortArrays {
-
public static void main(String args[]) {
-
Float[] score = {5.0f, 10.0f, 0.0f, 1.0f, 15.0f};
-
ArrayList<Float> values = new ArrayList<Float>();
-
HashMap<Float, Integer> originalPos = new HashMap<Float, Integer>();
-
for(int i=score.length-1;i>=0;i--) {
-
originalPos.put(score[i], i);
-
values.add(score[i]);
-
}
-
Collections.sort(values);
-
for(int i = values.size()-1; i >= 0; i--) {
-
float val = values.get(i);//No need for typecasting
-
System.out.print("" + val + " ");
-
System.out.println(originalPos.get(val));
-
}
-
}
-
}
-
Remember 1.5 has autoboxing and unboxing. Also make use of generic types - import java.util.*;
-
public class SortArrays {
-
public static void main(String args[]) {
-
Float[] score = {5.0f, 10.0f, 0.0f, 1.0f, 15.0f};
-
ArrayList<Float> values = new ArrayList<Float>();
-
HashMap<Float, Integer> originalPos = new HashMap<Float, Integer>();
-
for(int i=score.length-1;i>=0;i--) {
-
originalPos.put(score[i], i);
-
values.add(score[i]);
-
}
-
Collections.sort(values);
-
for(int i = values.size()-1; i >= 0; i--) {
-
float val = values.get(i);//No need for typecasting
-
System.out.print("" + val + " ");
-
System.out.println(originalPos.get(val));
-
}
-
}
-
}
-
Thanks for the solution, it works now.
Thanks for the solution, it works now.
Anytime, huiling25. One day I shall ask what your user name means.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
);
|
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...
|
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...
|
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...
| |
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;
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |