gpt4 book ai didi

java - 调用插入排序方法时出错以及如何打印数组列表

转载 作者:行者123 更新时间:2023-12-01 14:02:47 24 4
gpt4 key购买 nike

我创建了一个程序,它将温度( double )和日期(字符串)存储在数组列表中,然后使用类似的接口(interface)和插入排序对数组列表进行排序。现在我剩下的就是调用插入排序方法并打印出数组列表的原始顺序和排序顺序。然而,当我调用插入排序方法时,它给我一个错误,指出“无效的方法声明;需要返回类型”。为什么我会收到此错误?我的打印语句是否正确打印出原始数组列表?我将如何更新它以打印出排序后的数组列表?或者我可以从插入排序方法中打印出来吗?

这是我的代码:

import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;


public class DailyTemperature implements Comparable<DailyTemperature>
{
//variables
private Double temperature;
private String day;

//getTemp & setTemp methods
public double getTemp()
{
return temperature;
}

public void setTemp(double newTemp)
{
temperature = newTemp;
}

//getDay & setTEmp methods
public String getDay()
{
return day;
}

public void setDay(String newDay)
{
day = newDay;
}


public DailyTemperature(String day, double temperature)
{
this.day = day;
this.temperature = temperature;
}

public int compareTo(DailyTemperature other)
{
if (temperature < other.temperature) return -1;
if (temperature == other.temperature) return 0;
return 1;
}

public String toString()
{
return("Day of Week" + this.getDay() +
"Temperature" + this.getTemp());
}


}



import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

public class DailyTemperatureList
{
public static void main (String [] args)
{

ArrayList<DailyTemperature> dailytemps = new ArrayList<DailyTemperature>();

dailytemps.add(new DailyTemperature("Mon", 87.1));
dailytemps.add(new DailyTemperature("Tue", 88.3));
dailytemps.add(new DailyTemperature("Wed", 81.2));
dailytemps.add(new DailyTemperature("Thu", 84.0));
dailytemps.add(new DailyTemperature("Fri", 76.3));
}

public static <DailyTemperature extends Comparable<? super DailyTemperature>>
void insertionSort(ArrayList<DailyTemperature> dailytemps)
{
DailyTemperature temp = null;
int position = 0;

//loop from 2nd element on
for (int i = 1; i < dailytemps.size(); i++)
{
temp = dailytemps.get(i);
position = i;

while ( 0 < position && temp.compareTo(dailytemps.get(position - 1 )) < 0)
{
dailytemps.set(position, dailytemps.get(position - 1));
position--;
}
dailytemps.set(position,temp);
}
}

insertionSort(ArrayList<DailyTemperature> dailytemps);
System.out.println( dailytemps.toString() );





}

最佳答案

However when I call the insertionsort method it gives me an error stating "invalid method declaration; return type required". Why am i receiving this error?

您正在 DailyTemperatureList 类内部但在任何无法编译的方法外部调用insertionSort()。我将调用移至 main() 方法中,以便它可以编译,请参见下文。

Also is my print statement correct to print out the original arraylist? How would i update it to print out the sorted arraylist? Or can i print that out from the insertionsort method?

在调用insertionSort()之前打印出原始列表,排序后打印出排序后的列表:

System.out.println( "original list:" );
System.out.println( dailytemps.toString() );

insertionSort( dailytemps);
System.out.println( "sorted list:" );

输出:

original list: [Day of WeekMonTemperature87.1, Day of WeekTueTemperature88.3, Day of WeekWedTemperature81.2, Day of WeekThuTemperature84.0, Day of WeekFriTemperature76.3] 

sorted list: [Day of WeekFriTemperature76.3, Day of WeekWedTemperature81.2, Day of WeekThuTemperature84.0, Day of WeekMonTemperature87.1, Day of WeekTueTemperature88.3]

更正 DailyTemperatureList 类:

import java.io.*;
import java.util.Collections;
import java.util.Scanner;
import java.util.ArrayList;

public class DailyTemperatureList
{
public static void main (String [] args)
{

ArrayList<DailyTemperature> dailytemps = new ArrayList<DailyTemperature>();

dailytemps.add(new DailyTemperature("Mon", 87.1));
dailytemps.add(new DailyTemperature("Tue", 88.3));
dailytemps.add(new DailyTemperature("Wed", 81.2));
dailytemps.add(new DailyTemperature("Thu", 84.0));
dailytemps.add(new DailyTemperature("Fri", 76.3));

System.out.println( "original list:" );
System.out.println( dailytemps.toString() );

insertionSort( dailytemps);
System.out.println( "sorted list:" );
System.out.println( dailytemps.toString() );



}

//public static <DailyTemperature extends Comparable<? super DailyTemperature>>
static void insertionSort(ArrayList<DailyTemperature> dailytemps)
{
DailyTemperature temp = null;
int position = 0;

//loop from 2nd element on
for (int i = 1; i < dailytemps.size(); i++)
{
temp = dailytemps.get(i);
position = i;

while ( 0 < position && temp.compareTo(dailytemps.get(position - 1 )) < 0)
{
dailytemps.set(position, dailytemps.get(position - 1));
position--;
}
dailytemps.set(position,temp);
}
}






}

关于java - 调用插入排序方法时出错以及如何打印数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19213143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com