Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 歌曲数组中歌曲年份的二进制搜索_Java_Arrays_Binary Search - Fatal编程技术网

Java 歌曲数组中歌曲年份的二进制搜索

Java 歌曲数组中歌曲年份的二进制搜索,java,arrays,binary-search,Java,Arrays,Binary Search,我正在尝试获取代码来搜索特定年份制作的歌曲 我曾尝试实现自己的二进制搜索代码,但它并没有打印出当年制作的所有歌曲,只有一首 类别: 测试仪等级: public class MusicV3Tester2{ public static void main(String[]args) { int find = 0; MusicV3[] songs = new MusicV3[10]; MusicV3[] sortedSongs = new MusicV3[songs.le

我正在尝试获取代码来搜索特定年份制作的歌曲

我曾尝试实现自己的二进制搜索代码,但它并没有打印出当年制作的所有歌曲,只有一首 类别:

测试仪等级:

 public class MusicV3Tester2{
 public static void main(String[]args)
 {
    int find = 0;
    MusicV3[] songs = new MusicV3[10];
    MusicV3[] sortedSongs = new MusicV3[songs.length];
    songs[0] = new MusicV3("Sugar", 2014, "Maroon 5");
    songs[1] = new MusicV3("Mercy", 2016, "Shawn Mendes");
    songs[2] = new MusicV3("Shape of You", 2017, "Ed Sheeran");
    songs[3] = new MusicV3("Photograph", 2014, "Ed Sheeran");
    songs[4] = new MusicV3("Closer", 2016, "The Chainsmokers");
    songs[5] = new MusicV3("Galway Girl", 2017, "Ed Sheeran");
    songs[6] = new MusicV3("Counting Stars", 2013, "OneRepublic");
    songs[7] = new MusicV3("7 Years", 2015, "Lukas Graham");
    songs[8] = new MusicV3("Night Changes", 2014, "One Direction");
    songs[9] = new MusicV3("What Makes You Beautiful", 2011, "One Direction");

    printSongs(songs);
    System.out.println();

    sortedSongs = sortByYear(songs);
    System.out.println("Song list sorted by year:");
    //printSongs(sortedSongs);
    System.out.println();

    System.out.println("Searching for the year: 2000");
    find = findYear(songs, 2000);
    if(find != -1){
        System.out.println("We found songs made in the year 2000 in the song list: ");
        System.out.println(sortedSongs[find]);
    }
    else
        System.out.println("Songs made in the year 2000 are not in the song list");
    System.out.println();

    System.out.println("Searching for the year: 2014");
    find = findYear(songs, 2014);
    if(find != -1){
        System.out.println("We found songs made in the year 2014 in the song list: ");
        System.out.println(sortedSongs[find]);
    }
    else
        System.out.println("Songs made in the year 2014 are not in the song list");
    System.out.println();
}

public static void printSongs(MusicV3[] s)
{
    System.out.println("Song                      Year   Artist");
    System.out.println("-------------------------------------------------------");
    for(int i = 0; i < s.length; i++)
        System.out.println(s[i]);
    }

public static MusicV3[] sortByYear(MusicV3 [] movies){
    MusicV3[] sortedList = movies;
    int i;
    int k;
    int posmax;
    MusicV3 temp;
    for ( i = movies.length - 1 ; i >= 0 ; i-- )
    {
       posmax = 0;
         for ( k = 0 ; k <= i ; k++ )
         {
             if (movies[k].getYear()> movies[posmax].getYear())
             posmax = k;
            }

            temp = movies[i];
            movies[i] = movies[posmax];
            movies[posmax] = temp;
}
return sortedList;
}

public static int findYear(MusicV3[] songs, int year){
 int high = songs.length;
 int low = -1;
 int probe;

 while (high - low > 1)
 {
 probe = ( high + low ) / 2;
if (songs[probe].getYear() > year)
  high = probe;
else 
 low = probe;
} 

if(low >= 0 && songs[low].getYear() == year){
    return low;
}
else{
return -1;
}
}
public class MusicV3Tester2{
公共静态void main(字符串[]args)
{
int find=0;
MusicV3[]歌曲=新音乐v3[10];
MusicV3[]sortedSongs=新音乐v3[songs.length];
歌曲[0]=新音乐3(“糖”,2014,“栗色5”);
歌曲[1]=新音乐3(“Mercy”,2016,“Shawn Mendes”);
歌曲[2]=新音乐3(“你的形状”,2017,“Ed Sheeran”);
歌曲[3]=新音乐3(“照片”,2014年,“Ed Sheeran”);
歌曲[4]=新音乐3(“Closer”,2016,“The Chain Smokers”);
歌曲[5]=新音乐3(“高威女孩”,2017年,“Ed Sheeran”);
歌曲[6]=新音乐3(“数星”,2013,“OneRepublic”);
歌曲[7]=新音乐3(“7年”,2015年,“卢卡斯·格雷厄姆”);
歌曲[8]=新音乐3(“夜变”,2014,“一个方向”);
歌曲[9]=新音乐3(“什么让你美丽”,2011,“一个方向”);
印刷歌曲(歌曲);
System.out.println();
分类歌曲=分类歌曲(歌曲);
System.out.println(“按年份排序的歌曲列表:”);
//印刷歌曲(分类歌曲);
System.out.println();
System.out.println(“搜索年份:2000”);
find=findYear(songs,2000年);
如果(查找!=-1){
println(“我们在歌曲列表中找到了2000年制作的歌曲:”;
System.out.println(分类歌曲[find]);
}
其他的
System.out.println(“2000年制作的歌曲不在歌曲列表中”);
System.out.println();
System.out.println(“搜索年份:2014”);
find=findYear(songs,2014);
如果(查找!=-1){
System.out.println(“我们在歌曲列表中找到了2014年制作的歌曲:”);
System.out.println(分类歌曲[find]);
}
其他的
System.out.println(“2014年制作的歌曲不在歌曲列表中”);
System.out.println();
}
公共静态无效打印歌曲(MusicV3[]s)
{
System.out.println(“宋年艺术家”);
System.out.println(“--------------------------------------------------------------”);
对于(int i=0;i=0;i--)
{
posmax=0;
对于(k=0;k部电影[posmax].getYear())
posmax=k;
}
temp=电影[i];
电影[i]=电影[posmax];
电影[posmax]=临时;
}
返回分类列表;
}
公共静态int findYear(音乐3[]首歌曲,int年){
int high=歌曲长度;
int低=-1;
int探针;
而(高-低>1)
{
探头=(高+低)/2;
如果(歌曲[probe].getYear()>year)
高=探头;
其他的
低=探头;
} 
如果(低>=0&&songs[low].getYear()==year){
低回报;
}
否则{
返回-1;
}
}
}


它应该从数组中获取2000年和2014年制作的所有歌曲,但由于某些原因,2000年的搜索是正确的,而不是2014年,它只是说2014年只制作了一首歌曲,而有多首歌曲时,看起来你没有检查
歌曲[probe].getYear()==年
findYear
中的while循环期间

也许:

public static int findYear(MusicV3[] songs, int year) {
    int high = songs.length;
    int low = -1;
    int probe = -1;

    while (high - low > 1) {
        probe = (high + low) / 2;
        int probeSongYear = songs[probe].getYear();

        if (probeSongYear == year) {
            break;
        } else if (probeSongYear > year)
            high = probe;
        else
            low = probe;
    }

    if (probe >= 0 && songs[probe].getYear() == year) {
        while (songs[--probe].getYear() == year) {
        }
        return probe + 1;
    }

    return -1;
}
不要忘记添加“
find=
”,如下所示:

    System.out.println("Searching for the year: 2014");
    find = /* <--- ADD THIS */ findYear(songs, 2014);

findYear
中的while循环中,似乎没有检查
songs[probe].getYear()==year

也许:

public static int findYear(MusicV3[] songs, int year) {
    int high = songs.length;
    int low = -1;
    int probe = -1;

    while (high - low > 1) {
        probe = (high + low) / 2;
        int probeSongYear = songs[probe].getYear();

        if (probeSongYear == year) {
            break;
        } else if (probeSongYear > year)
            high = probe;
        else
            low = probe;
    }

    if (probe >= 0 && songs[probe].getYear() == year) {
        while (songs[--probe].getYear() == year) {
        }
        return probe + 1;
    }

    return -1;
}
不要忘记添加“
find=
”,如下所示:

    System.out.println("Searching for the year: 2014");
    find = /* <--- ADD THIS */ findYear(songs, 2014);

在我看来,独立完成每一件事情并不总是最好的方法

至于我,我会制作一个
HashMap
,将年份存储到歌曲列表映射中

这就是最终代码的样子

HashMap<Integer, ArrayList<MusicV3>> songs = new HashMap<>();

void addSong(MusicV3 song) {
    ArrayList<MusicV3> list = songs.get(song.year);

    if (list == null) {
        list = new ArrayList<>();
        songs.put(song.year, list);
    }

    list.add(song);
}

ArrayList<MusicV3> findSongsByYear(int year) {
    ArrayList<MusicV3> list = songs.get(year);
    if (list != null)
        return list;
    else
        return new ArrayList<>();
}
HashMap歌曲=新建HashMap();
void addSong(MusicV3歌曲){
ArrayList list=songs.get(song.year);
if(list==null){
列表=新的ArrayList();
songs.put(song.year,list);
}
列表。添加(歌曲);
}
ArrayList findSongsByYear(国际年){
ArrayList=songs.get(年份);
如果(列表!=null)
退货清单;
其他的
返回新的ArrayList();
}

在我看来,独立实现每一件事情并不总是最好的方法

至于我,我会制作一个
HashMap
,将年份存储到歌曲列表映射中

这就是最终代码的样子

HashMap<Integer, ArrayList<MusicV3>> songs = new HashMap<>();

void addSong(MusicV3 song) {
    ArrayList<MusicV3> list = songs.get(song.year);

    if (list == null) {
        list = new ArrayList<>();
        songs.put(song.year, list);
    }

    list.add(song);
}

ArrayList<MusicV3> findSongsByYear(int year) {
    ArrayList<MusicV3> list = songs.get(year);
    if (list != null)
        return list;
    else
        return new ArrayList<>();
}
HashMap歌曲=新建HashMap();
void addSong(MusicV3歌曲){
ArrayList list=songs.get(song.year);
if(list==null){
列表=新的ArrayList();
songs.put(song.year,list);
}
列表。添加(歌曲);
}
ArrayList findSongsByYear(国际年){
ArrayList=songs.get(年份);
如果(列表!=null)
退货清单;
其他的
返回新的ArrayList();
}

这是一个课程,你只能按他们的方式做,这是他们的方式lol。我会熟练地使用arraylist,但我们必须使用一个超级烦人的数组。啊,好吧,现在我明白了原因。这是一个课程,你只能按他们的方式做,这是他们的方式lol。我会熟练地使用arraylist,但我们必须使用一个非常烦人的数组。啊,好吧,现在我明白了原因。是的,我的最后一部分,只是在复制时不小心把它关掉了。所以我所做的只是添加了
find=
部分,现在它可以工作了,但它只输出了2014年制作的第一首歌,我必须在静态主void中添加什么才能让它打印出2014年制作的所有歌曲更新的主描述编辑了答案中的findYear实现——它现在返回2014年制作的第一首歌曲。要全部打印,请使用编辑中添加的代码。是的,我的最后一部分,只是在复制时不小心遗漏了。我所做的就是这样