There are no errors but it still won't work. I can't figure out what's wrong. We still haven't discussed the concept of nodes in class so I have trouble understanding it.
It's output should be:
Skinny Love- Birdy
Happier- Ed Sheeran
Someday- Chelsea Cutler
public class HandsOnAct1 {
static LinkedList songs = new LinkedList();
static LinkedList artists = new LinkedList();
static LinkedList playlist = new LinkedList();
public static void Merge(){
int element= songs.size();
int number=0;
System.out.println(element);
if (number !=element){
playlist.add(number, songs);
playlist.add(number,artists);
number++;
}
System.out.println(playlist);
}
public static void main(String[] args) {
songs.add("Skinny Love");
songs.add("Happier");
songs.add("Sometimes");
artists.add("Birdy");
artists.add("Ed Sheeran");
artists.add("Chelsea Cutler");
Merge();
}
}
Here is an example using recursion as i understand it was a pre-requisite?
static LinkedList<String> songs = new LinkedList<>();
static LinkedList<String> artists = new LinkedList<>();
static LinkedList<String> playlist = new LinkedList<>();
songs.add("Skinny Love");
songs.add("Happier");
songs.add("Sometimes");
artists.add("Birdy");
artists.add("Ed Sheeran");
artists.add("Chelsea Cutler");
int index = 0;
static void merge() {
// if you want/need checks
if ( index >= artists.size()){
return;
}
playlist.add(songs.get(index) + " - " + artists.get(index));
System.out.println(playlist.get(index));
index++;
merge();
}
merge();
the main issue with your own code is that you need to call merge again from inside it and either move the index outside the function as i have done or take it in as a parameter like public static void merge(int index)
. you will then call it from inside like merge(index++)
.
That's a matter of opinion i guess. (if you want you can read on pure/impure functions and imperative/functional programming which is the difference)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.