- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我构建了一个名为 Melody 的类,它从文件中读取以下几行并将它们存储到名为 Note 类型的音符的数组中。这些列是:以 1/100 秒为单位的时间、音符编号、速度、长度。
0 60 100 24
25 72 100 24
100 60 100 24
50 60 100 24
75 72 100 24
对于我的类(class)作业,我需要实现 Comparable 接口(interface)。我需要使我的 Note 类可比较,以便笔记根据时间排序,如果两个笔记同时出现,那么我需要将笔记编号较小的笔记放在前面。我需要一些帮助,因为我在尝试正确实现 comparaTo 方法并打印结果时遇到了困难。我将不胜感激任何帮助,提前致谢。
这是我的 Note 类
public class Note implements Comparable <Note> {
private int time;
private int noteNumber;
private int velocity;
private int length;
public Note (int time,int noteNumber,int velocity,int lenght){
this.time = time;
this.noteNumber = noteNumber;
this.velocity = velocity;
this.length = lenght;
}
public String toString()
{
String s = time +" "+ noteNumber + " " + velocity + " " + length;
return s;
}
@Override
public int compareTo(Note o) {
return Integer.compare(time, o.time);
}
}
这是我的旋律课
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Melody {
Note [] notes = new Note[5];
public Melody() throws IOException{
FileReader fr = new FileReader ("/Users/enricomomo/Desktop/Text/file2.txt");
BufferedReader br = new BufferedReader (fr); //Info stored into a buffer
String line = br.readLine(); //Lines are read from the buffer and stored into a variable
int lineCounter = 0;
while ( line != null )//When reached the end of the file the while loop stops
{
StringTokenizer st = new StringTokenizer(line, " ");
int time = Integer.parseInt(st.nextToken());
int noteNumber = Integer.parseInt(st.nextToken());
int velocity = Integer.parseInt(st.nextToken());
int length = Integer.parseInt(st.nextToken());
System.out.println(line + " " + lineCounter);
Note n = new Note(time,noteNumber,velocity,length);
notes[lineCounter] = n;
line = br.readLine();
lineCounter ++;
}
br.close();
}
public void contet(){
for ( int i = 0; i < notes.length; i ++)
{
System.out.println(notes[i]);
}
}
public String toString()
{
String rtn = "";
//Code to create a String version of the object
for ( int i = 0; i < notes.length; i ++)
{
rtn += "\n";
}
return rtn;
}
}
这是我的测试课
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
Melody m = new Melody();
System.out.print(m);
m.contet();
}
}
最佳答案
由于 time
和 noteNumber
是 int
:
@Override
public int compareTo(Note o) {
if (time == o.time)
return Integer.compare(noteNumber, o.noteNumber);
else
return Integer.compare(time, o.time);
}
compareTo
需要返回负数、零或正数,
取决于第一项是小于、等于还是大于第二项。
填充 Note
对象数组后,调用:
Arrays.sort(notes);
关于java - 我需要帮助才能正确实现 Comparable 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53818954/
我经常有一个 Comparator 类型,而我需要一个 Comparable 类型,反之亦然。是否有可重用的 JDK API 可以相互转换?类似的东西: public static Comp
我怎么能写这个 Comparator sort = (i1, i2) -> Boolean.compare(i2.isOpen(), i1.isOpen()); 像这样(代码不起作用): Compa
请帮助她。我有一个错误 Collections.sort(var4, new Comparator() { public int compare(TreeMap var1, TreeMa
学习 Kotlin,我试图了解 Java 的 Comparator接口(interface)有效 - 主要是 compare() 函数,这样我就可以利用它。 我已经尝试阅读 compare() 的文档
我有以下程序 List numbers = Arrays.asList("10", "68", "97", "9", "21", "12"); Collections.sort(numbers, (
我想根据嵌套类的属性对如下所示的列表进行排序。 class Test { private NestedClass nestedClass; private AnotherNes
我很好奇“Beyond Compare”的算法是如何工作的? 我猜想他们使用了一种标准的(众所周知的?)算法来实现“字符与字符”的比较。你知道这个算法的名字吗?谢谢 最佳答案 Beyond Compa
这个问题已经有答案了: How does the sort() method of the Collection class call the Comparable's compareTo()? (1
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicates: difference between compare() and compareTo() Java: What i
我被要求为某个类实现Comparable或Compartor,我们称之为V。 假设我有一个 V 的 Collection 或 Set(还不确定,但我认为这并不重要)。 V 有一个方法,可以评估它的“权
我正在查看Java8中实现的Comparator.comparing方法的源代码 这是代码 public static Comparator comparing( Function
假设我有一个类 ClassA,它的属性是 ClassB: public ClassA { private String attr; private ClassB classB; } p
我有一个自定义比较器,其比较逻辑如下: List l = new ArrayList(); l.add("tendercoupon"); l.add("giftcard
我正在努力实现一个处理 Comparator 和 Comparable 接口(interface)的层次结构。我不清楚的几件事: 如果我将比较器添加到比较器链中,这段代码究竟意味着什么 chain.a
正在关注 this question关于按另一个列表对列表进行排序,我尝试做同样的事情 - 但由于某种原因它对我不起作用。我错过了什么? List nums = Arrays.asList(5
假设我有一个像这样的领域模型: class Lecture { Course course; ... // getters } class Course { Teache
在表达式 > 中像这样的签名 public static > foo(T x) { ... } T的描述递归地依赖于Comparable . 如果T延伸Comparable ,和Comparable延
所有“数字”比较器(例如 Comparer.Default 、 Comparer.Default 等)返回 -1 的原因是什么? , 0或 1 ,但是 Comparer.Default和 Compar
(如果这是重复的,请指出正确的答案!我搜索并阅读了几个(> 5)个相关问题,但似乎没有一个是正确的。还查看了泛型常见问题解答和其他来源...) 当一个集合类接受一个比较器时,它应该具有 Compara
SBCL 1.3.1 综上所述,a是一个列表,'(7),b通过setq sbcl This is SBCL 1.3.1.debian, an implementation of ANSI Common
我是一名优秀的程序员,十分优秀!