为链表中的数组对象创建“排序”方法

时间:2022-05-16 07:16:30

I have a project that requires we create a an object array and then we place that in a linked list, I am kind of stuck because I have trouble writing/implementing my sort method that is supposed to sort the the linked list. Here is my code of how far I've gone. By the way, the object name is 'temperature'; Thank you.

我有一个项目要求我们创建一个对象数组,然后我们将它放在一个链表中,我有点卡住,因为我无法编写/实现我的排序方法,应该对链表进行排序。这是我离开的距离的代码。顺便说一句,对象名称是'温度';谢谢。

public class SelectionSort
{
   private static void SelectionSort (Temperature[] array, int size)
   { 
      for ( int i = 0; i < size - 1; i++ )
      { 
         int indexLowest = i; 
         for ( int j = i + 1; j < size; j++ )
         {
            if ( array[j] < array[indexLowest] ) 
               indexLowest = j;

            if ( array[indexLowest] != array[i] )
            { 
               Temperature temp = array[indexLowest];
               array[indexLowest] = array[i]; 
               array[i] = temp; 
            }// if
         }//for j
      }// for i 
   }// method 
}

1 个解决方案

#1


1  

I think, your problem is the line

我想,你的问题就在于此

if ( array[j] < array[indexLowest] )

Both - array[j] and array[indexLowest] - are of type Temperature, according to your method's signature. So they are not primitive types and thus cannot be compared with <. This obviously results in a compiler error, that you really should have told us.

根据您方法的签名,数组[j]和数组[indexLowest]都是Temperature类型。因此它们不是原始类型,因此无法与<。这显然会导致编译器错误,您应该告诉我们。

To compare objects like this, you have two possibilities:

要比较这样的对象,您有两种可能性:

1) Let the class Temperature implement Comparable<Temperature>. This interface will force you to add a method public int compareTo(Temperatue other) to your class Temperature. Implement this in the following way:

1)让类温度实现可比较 <温度> 。此界面将强制您向类Temperature添加方法public int compareTo(Temperatue other)。通过以下方式实现此目的:

@Override
public int compareTo(Temperatue other) {
     if (/* this is smaller than other */) {
        return -1;
    } else if (/* this is greater than other */) {
        return 1;
    } else {
        return 0;
    }
}

You could return any other positive or negative integer if you want to. Implement the comparisonyourself based on the fields in Temperature.

如果您愿意,可以返回任何其他正整数或负整数。根据温度字段实现自我比较。

Use this in your problematic line as:

在有问题的行中使用此作为:

if ( array[j].compareTo(array[indexLowest]) < 0 )

2) Write a Comparator for your class Temperature.

2)为你的班级温度写一个比较器。

public class TemperatureComparator implements Comparator<Temperature> {
    public int compare(Temperature t1, Temperature t2) {
        if (/* t1 is smaller than t2 */) {
            return -1;
        } else if (/* t1 is greater than t2 */) {
            return 1;
        } else {
            return 0;
        }
    }
}

The logic is similar. Now you can use this comparator in your sort method

逻辑是相似的。现在,您可以在sort方法中使用此比较器

private static void SelectionSort (Temperature[] array, int size) {
    Comparator<Temperature> comparator = new TemperatureComparator();
    ...
        if ( comparator.compare(array[j], array[indexLowest]) < 0 )
    ...
}

#1


1  

I think, your problem is the line

我想,你的问题就在于此

if ( array[j] < array[indexLowest] )

Both - array[j] and array[indexLowest] - are of type Temperature, according to your method's signature. So they are not primitive types and thus cannot be compared with <. This obviously results in a compiler error, that you really should have told us.

根据您方法的签名,数组[j]和数组[indexLowest]都是Temperature类型。因此它们不是原始类型,因此无法与<。这显然会导致编译器错误,您应该告诉我们。

To compare objects like this, you have two possibilities:

要比较这样的对象,您有两种可能性:

1) Let the class Temperature implement Comparable<Temperature>. This interface will force you to add a method public int compareTo(Temperatue other) to your class Temperature. Implement this in the following way:

1)让类温度实现可比较 <温度> 。此界面将强制您向类Temperature添加方法public int compareTo(Temperatue other)。通过以下方式实现此目的:

@Override
public int compareTo(Temperatue other) {
     if (/* this is smaller than other */) {
        return -1;
    } else if (/* this is greater than other */) {
        return 1;
    } else {
        return 0;
    }
}

You could return any other positive or negative integer if you want to. Implement the comparisonyourself based on the fields in Temperature.

如果您愿意,可以返回任何其他正整数或负整数。根据温度字段实现自我比较。

Use this in your problematic line as:

在有问题的行中使用此作为:

if ( array[j].compareTo(array[indexLowest]) < 0 )

2) Write a Comparator for your class Temperature.

2)为你的班级温度写一个比较器。

public class TemperatureComparator implements Comparator<Temperature> {
    public int compare(Temperature t1, Temperature t2) {
        if (/* t1 is smaller than t2 */) {
            return -1;
        } else if (/* t1 is greater than t2 */) {
            return 1;
        } else {
            return 0;
        }
    }
}

The logic is similar. Now you can use this comparator in your sort method

逻辑是相似的。现在,您可以在sort方法中使用此比较器

private static void SelectionSort (Temperature[] array, int size) {
    Comparator<Temperature> comparator = new TemperatureComparator();
    ...
        if ( comparator.compare(array[j], array[indexLowest]) < 0 )
    ...
}