如何在不使用collections.swap的情况下交换链表中的元素。

huangapple go评论83阅读模式
英文:

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&lt;Integer&gt; llist;
  public static void main(String[] args) {
    LinkedList&lt;Integer&gt; languages = new llist&lt;&gt;();
    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(&quot;LinkedList: &quot; + languages);
   	exchange(3,2);
   	System.out.println(&quot;LinkedList: &quot; + 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&lt;Integer&gt; languages = new llist&lt;&gt;();

Has an error - replace it with

llist = new LinkedList&lt;Integer&gt;();

So it becomes

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    private static LinkedList&lt;Integer&gt; llist;
    public static void main(String[] args) {
        llist = new LinkedList&lt;Integer&gt;();
        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(&quot;LinkedList: &quot; + llist);
        exchange(3,2);
        System.out.println(&quot;LinkedList: &quot; + llist);
    }

    public static void exchange(int i, int j) {
        llist.set(i, llist.set(j, llist.get(i)));
    }
}

Also

System.out.println(&quot;LinkedList: &quot; + languages);

should be:

System.out.println(&quot;LinkedList: &quot; + llist);

as you do not really use languages, but llist;

huangapple
  • 本文由 发表于 2020年10月25日 11:40:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64520123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定