英文:
How do I merge linked list alternately without using nodes? and by using recursion?
问题
以下是翻译好的内容:
没有错误,但仍无法工作。我无法弄清楚问题出在哪里。我们还没有在课堂上讨论过节点的概念,所以我很难理解它。
输出应为:
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();
}
}
英文:
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();
}
}
答案1
得分: 0
这是一个使用递归的示例,正如我理解的那样,这是一个先决条件。
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 (index >= artists.size()){
return;
}
playlist.add(songs.get(index) + " - " + artists.get(index));
System.out.println(playlist.get(index));
index++;
merge();
}
merge();
你自己的代码的主要问题在于,你需要从内部再次调用merge
函数,并且要么像我所做的那样将索引移到函数外部,要么将其作为参数传入,比如public static void merge(int index)
。然后你可以像这样从内部调用它:merge(index++)
。
这可能是一个观点问题。(如果你愿意,你可以阅读关于纯函数/非纯函数和命令式/函数式编程的内容,这是其中的区别。)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论