分离浮子和浮子;Java中对象中的字符串
我有一个二维对象,行中是字符串名,列中是浮点数 我想对浮点数进行排序,但当我将其发送到bubbleSort函数时,它会给我一个无法使用<或>的错误 我如何在数字的名称旁边排序 对于Java中的对象,我应该导入什么分离浮子和浮子;Java中对象中的字符串,java,sorting,object,import,Java,Sorting,Object,Import,我有一个二维对象,行中是字符串名,列中是浮点数 我想对浮点数进行排序,但当我将其发送到bubbleSort函数时,它会给我一个无法使用的错误 我如何在数字的名称旁边排序 对于Java中的对象,我应该导入什么 public static void bubbleSort(Object student[][] , int length) { int w,x; float place; for(w=length-1; w>0 ;w--) {
public static void bubbleSort(Object student[][] , int length)
{
int w,x;
float place;
for(w=length-1; w>0 ;w--)
{
for(x=0; x<w ; x++)
{
if(student[x][1]<student[x+1][1])
{
place=student[x][1];
student[x][1]=student[x+1][1];
student[x+1][1]=place;
}
}
}
}//bubbleSort
publicstaticvoidbubblesort(对象student[],int-length)
{
int w,x;
浮置;
对于(w=长度-1;w>0;w--)
{
对于(x=0;x,由于数组包含对象而不是基元,因此不能使用<和>…但是如果您事先将对象强制转换为浮点,则可以。以下是一些不相关的示例代码,以便您可以查看强制转换是如何实现的…您可以从中独立完成
Object o;
o = 3.1415F;
System.out.println(o);
System.out.println("Casting to float and printing: " + (float) o);
Object p = 4.1415F;
System.out.println("p is " + p);
System.out.println("p < o? " + ((float) p < (float) o));
对象o;
o=3.1415F;
系统输出打印ln(o);
System.out.println(“铸造到浮子和印刷:”+(浮子)o);
物体p=4.1415F;
System.out.println(“p是”+p);
System.out.println(“p
学生[x][1]
和学生[x+1][1]
属于对象类型
您不能在之类的算术运算中使用它,请先将值转换为浮点类型,然后进行比较
public static void bubbleSort(Object student[][] , int length)
{
int w,x;
float place;
for(w=length-1; w>0 ;w--)
{
for(x=0; x<w ; x++)
{
float fValue1 = (float)student[x][1];
float fValue2 = (float)student[x+1][1];
if(fValue1 < fValue2)
{
place=(float) student[x][1];
student[x][1]=student[x+1][1];
student[x+1][1]=place;
}
}
}
}//bubbleSort
publicstaticvoidbubblesort(对象student[],int-length)
{
int w,x;
浮置;
对于(w=长度-1;w>0;w--)
{
这是我的bubbleSort函数:公共静态void bubbleSort(Object student[],int length){int w,x;float place;for(w=length-1;w>0;w--){for(x=0;xYou不需要导入任何东西来使用object。将其编辑到问题中,并将其格式化为代码;这是非常不可读的。虽然我已经这样做了^^^谢谢,但这是一个错误,您不能从object转换为float。