I want to know how I can store the output in an array and then sort its values.
public static void main(String[] args) {
int i;
float[] npoints = new float[10];
float[] points = new float[10];
points[0]=(float) 0.3;
points[1]=(float) 0.2;
points[2]=(float) 0.4;
points[3]=(float) 0.5;
points[4]=(float) 0.6;
points[5]=(float) 0.0123;
for(i=0;i<6;i++)
{
if(points[i]!=0.0)
{
npoints[i]=points[i];
System.out.println(i+":"+points[i]);
}
}
System.out.println(npoints[i]);
}
}
Output:
0:0.3
1:0.2
2:0.4
3:0.5
4:0.6
5:0.0123
0.0
BUILD SUCCESSFUL (total time: 0 seconds)
Use
Arrays#sort():Note that this method sorts the array in place, meaning it will modify the array that you pass as an argument.
If you want to copy the array, you can use
Arrays#copyOf, and then sort the copy:DEMO.