英文:
How to swap elements in a linked list without using collections.swap
问题
我是Java的初学者。我想要能够使用索引值交换元素,无论是使用自己的方法还是不使用collections.swap。
import java.util.LinkedList;
import java.util.Scanner;
class Main {
private static LinkedList<Integer> llist;
public static void main(String[] args) {
llist = new LinkedList<>();
Scanner s = new Scanner(System.in);
// 向链表中添加元素
llist.add(5);
llist.add(4);
llist.add(3);
llist.add(2);
System.out.println("LinkedList: " + llist);
exchange(3, 2);
System.out.println("LinkedList: " + llist);
}
public static void exchange(int i, int j) {
llist.set(i, llist.set(j, llist.get(i)));
}
}
在构建这个程序时,不会出现错误,但是当我运行它时,会出现java.lang.NullPointerException
错误。
英文:
I'm a beginner at java. I want to be able to swap elements using index value while using my own method or without using collections.swap
import java.util.LinkedList;
import java.util.Scanner;
class Main {
private static LinkedList<Integer> llist;
public static void main(String[] args) {
LinkedList<Integer> languages = new llist<>();
Scanner s = new Scanner(System.in);
// add elements in the linked list
llist.add(5);
llist.add(4);
llist.add(3);
llist.add(2);
System.out.println("LinkedList: " + languages);
exchange(3,2);
System.out.println("LinkedList: " + languages);
}
public static void exchange(int i, int j) {
llist.set(i, llist.set(j, llist.get(i)));
}
}
Whenever I build this one there is no error that pops up, but when I run it, it gives me the java.lang.NullPointerException.
答案1
得分: 0
你的代码行:
LinkedList<Integer> languages = new llist<>();
有一个错误 - 将其替换为:
llist = new LinkedList<Integer>();
因此变成:
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
private static LinkedList<Integer> llist;
public static void main(String[] args) {
llist = new LinkedList<Integer>();
Scanner s = new Scanner(System.in);
// 在链表中添加元素
llist.add(5);
llist.add(4);
llist.add(3);
llist.add(2);
System.out.println("LinkedList: " + llist);
exchange(3, 2);
System.out.println("LinkedList: " + llist);
}
public static void exchange(int i, int j) {
llist.set(i, llist.set(j, llist.get(i)));
}
}
另外
System.out.println("LinkedList: " + languages);
应改为:
System.out.println("LinkedList: " + llist);
因为你实际上并没有使用 languages
,而是使用了 llist
。
英文:
Your line:
LinkedList<Integer> languages = new llist<>();
Has an error - replace it with
llist = new LinkedList<Integer>();
So it becomes
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
private static LinkedList<Integer> llist;
public static void main(String[] args) {
llist = new LinkedList<Integer>();
Scanner s = new Scanner(System.in);
// add elements in the linked list
llist.add(5);
llist.add(4);
llist.add(3);
llist.add(2);
System.out.println("LinkedList: " + llist);
exchange(3,2);
System.out.println("LinkedList: " + llist);
}
public static void exchange(int i, int j) {
llist.set(i, llist.set(j, llist.get(i)));
}
}
Also
System.out.println("LinkedList: " + languages);
should be:
System.out.println("LinkedList: " + llist);
as you do not really use languages
, but llist
;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论