我需要帮助以正确实现Comparable接口

时间:2018-12-17 16:09:41

标签: java comparable compareto

我建立了一个名为Melody的类,该类从文件中读取以下行并将其存储到称为Notes类型的notes的数组中。这些列是:以1/100秒为单位的时间滴答声,音符编号,力度,长度。

0 60 100 24
25 72 100 24
100 60 100 24
50 60 100 24
75 72 100 24

对于我的课程工作,我需要实现Comparable接口。我需要将我的Note类设为Comparable,以便基于时间对笔记进行排序,如果两个笔记同时出现,那么我需要先将笔记编号较低的笔记放在首位。我需要一些帮助,因为我一直无法正确实现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();
    } 
}

2 个答案:

答案 0 :(得分:2)

由于and t.date in (select max(t1.date) from table_name t1 where t1.id in (select st.id from some_table st) ) and t.date in (select max(t1.date) from table_name t1 where t1.id in (select regexp_substr(%s, '[^,]+', 1, level) from dual connect by level <= regexp_count(%s, ',') + 1 ) ) 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

答案 1 :(得分:-2)

尝试一下:

public class Note implements Comparable <Note>
{
  ...

  @Override
  public int compareTo(Note o)
  {
    int result;
    result = time - o.time;
    if (result == 0)
      result = noteNumber - o.noteNumber;
    if (result == 0)
      result = velocity - o.velocity;
    return (result);
  }  
}

之后,您需要对数组进行排序:

public Melody() throws IOException
{
  ...

  Arrays.sort(notes);

}