我试图按照递增顺序对ArrayList
进行排序以引用某个变量。这是一个问题。
q5:创建一个名为
Snow
的公共类,其私有实例变量vast
,prior
,ethnic
和remarkable
各有{{1}类型}。您可以在此课程中添加您想要的任何其他方法和变量。在
int
之外(在问题集类中)编写一个名为Snow
的公共静态方法,该方法将sortSnow
个Snows作为参数并返回void。此方法将按变量ArrayList
按递增顺序对输入进行排序
这就是我写的。
remarkable
我不明白错误的含义。 autolab给了我这个错误:
无法找到课程
public class snow implements Comparable<snow> { private int vast; private int prior; private int ethnic; private int remarkable; public snow( int vast , int prior, int ethnic ,int remarkable) { this.vast=vast; this.prior = prior; this.ethnic = ethnic; this.remarkable = remarkable; } public int getEthnic() { return ethnic; } public void setEthnic(int ethnic) { this.ethnic = ethnic; } public int getPrior() { return prior; } public void setPrior(int prior) { this.prior = prior; } public int getVast() { return vast; } public void setVast(int vast) { this.vast = vast; } public int getRemarkable() { return remarkable; } public void setRemarkable(int remarkable) { this.remarkable = remarkable; } public int compareTo(snow compareSnow) { // TODO Auto-generated method stub int compareThese = ((snow) compareSnow).getRemarkable(); //ascending order return this.remarkable - compareThese; } } public static void sortSnow(ArrayList<snow>input){ Collections.sort(input); }
答案 0 :(得分:1)
Java区分大小写,即雪不是雪不是。将您的班级重命名为Snow,然后重试。此外,它是ArrayList而不是arraylist。
然后,要对列表进行排序,您可以使用public static TimeSpan ParseDuration(string input)
{
string[] inputParts = input.Split(':');
string ddString = inputParts[0];
string hhString = inputParts[1];
string mmString = inputParts[2];
string ssString = inputParts[3];
int dd = int.Parse(ddString, CultureInfo.InvariantCulture);
int hh = int.Parse(hhString, CultureInfo.InvariantCulture);
int mm = int.Parse(mmString, CultureInfo.InvariantCulture);
int ss = int.Parse(ssString, CultureInfo.InvariantCulture);
return new TimeSpan(dd, hh, mm, ss);
}
。
答案 1 :(得分:0)
我认为这是你要实现的目标
将以下代码保存在名为&#34; Snow.java&#34;的文件中。编译它并尝试运行它。
import java.util.ArrayList;
import java.util.Collections;
//As ".java" file can contain only single public java class
//I made Problem set class non-public so we can use its main method
//to run and see output
class ProblemSet {
public static void main(String[] args) {
Snow one = new Snow(1,1,1,1);
Snow two = new Snow(1,1,1,2);
Snow three = new Snow(1,1,1,3);
Snow four = new Snow(1,1,1,4);
Snow five = new Snow(1,1,1,5);
Snow six = new Snow(1,1,1,6);
ArrayList arrayList = new ArrayList();
arrayList.add(one);
arrayList.add(three);
arrayList.add(five);
arrayList.add(two);
arrayList.add(six);
arrayList.add(four);
System.out.println("Without sort");
System.out.println(arrayList);
sortSnow(arrayList);
System.out.println("With sort");
System.out.println(arrayList);
}
//this is your static method which takes argument as array list of Snow
//And it applies sorting logic based on compareTo method which you wrote
//in Snow class. As per java best practice Class name should start with
//Upper case letters and follow camel casing I renamed your class from
//"snow" to "Snow"
public static void sortSnow(ArrayList<Snow> input){
Collections.sort(input);
}
}
//This is you public class Snow
//If you want to keep it in separate java file put it
public class Snow implements Comparable<Snow> {
private int vast;
private int prior;
private int ethnic;
private int remarkable;
public Snow(int vast, int prior, int ethnic, int remarkable) {
this.vast = vast;
this.prior = prior;
this.ethnic = ethnic;
this.remarkable = remarkable;
}
public int getEthnic() {
return ethnic;
}
public void setEthnic(int ethnic) {
this.ethnic = ethnic;
}
public int getPrior() {
return prior;
}
public void setPrior(int prior) {
this.prior = prior;
}
public int getVast() {
return vast;
}
public void setVast(int vast) {
this.vast = vast;
}
public int getRemarkable() {
return remarkable;
}
public void setRemarkable(int remarkable) {
this.remarkable = remarkable;
}
public int compareTo(Snow compareSnow) {
// TODO Auto-generated method stub
int compareThese = ((Snow) compareSnow).getRemarkable();
//ascending order
return this.remarkable - compareThese;
}
//This is added because when you use array list to print
//it will print remarkable of particular Snow object
@Override
public String toString() {
return String.valueOf(remarkable);
}
}