|
-
Jan 29th, 2007, 09:15 PM
#1
Thread Starter
Junior Member
[RESOLVED]My program cannot work for float array
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:
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)));
}
}
}
Last edited by huiling25; Jan 30th, 2007 at 01:39 AM.
-
Jan 30th, 2007, 01:38 AM
#2
Thread Starter
Junior Member
Re: HELP!! My program cannot work for float array
I have found a solution to this problem, here is the code:
Code:
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));
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|