【问题标题】:java Linked List for swapping two numbersjava链表交换两个数字
【发布时间】:2022-02-18 20:16:37
【问题描述】:

我的代码出现错误。错误是: 错误:类 SwapNodes 是公共的,应在名为 SwapNodes.java 的文件中声明 公共类 SwapNodes {
^ Main.java:104:错误:内部类 SwapNodes.Main 中的非法静态声明 公共静态 void main(String[] args) {
^ 修饰符 'static' 只允许在常量变量声明中 我的程序如下:

public class SwapNodes {  
      
    //Represent a node of the singly linked list  
    class Node{  
        int data;  
        Node next;  
          
        public Node(int data) {  
            this.data = data;  
            this.next = null;  
        }  
    }  
   
    //Represent the head and tail of the singly linked list  
    public Node head = null;  
    public Node tail = null;  
      
    //addNode() will add a new node to the list  
    public void addNode(int data) {  
        //Create a new node  
        Node newNode = new Node(data);  
          
        //Checks if the list is empty  
        if(head == null) {  
            //If list is empty, both head and tail will point to new node  
            head = newNode;  
            tail = newNode;  
        }  
        else {  
            //newNode will be added after tail such that tail's next will point to newNode  
            tail.next = newNode;  
            //newNode will become new tail of the list  
            tail = newNode;  
        }  
    }  
      
    //swap() will swap the given two nodes  
    public void swap(int n1, int n2){  
        Node prevNode1 = null, prevNode2 = null, node1 = head, node2 = head;  
          
        //Checks if list is empty  
        if(head == null) {  
            return;  
        }  
          
        //If n1 and n2 are equal, then list will remain the same  
        if(n1 == n2)  
            return;  
          
        //Search for node1  
        while(node1 != null && node1.data != n1){  
            prevNode1 = node1;  
            node1 = node1.next;  
        }  
          
        //Search for node2  
        while(node2 != null && node2.data != n2){  
            prevNode2 = node2;  
            node2 = node2.next;  
        }  
          
        if(node1 != null && node2 != null) {  
              
            //If previous node to node1 is not null then, it will point to node2  
            if(prevNode1 != null)  
                prevNode1.next = node2;          
            else  
                head  = node2;  
              
            //If previous node to node2 is not null then, it will point to node1  
            if(prevNode2 != null)  
                prevNode2.next = node1;  
            else  
                head  = node1;  
              
            //Swaps the next nodes of node1 and node2  
            Node temp = node1.next;   
            node1.next = node2.next;   
            node2.next = temp;       
        }      
        else {  
            System.out.println("Swapping is not possible");  
        }  
    }  
      
    //display() will display all the nodes present in the list  
    public void display() {  
        //Node current will point to head  
        Node current = head;  
          
        if(head == null) {  
            System.out.println("List is empty");  
            return;  
        }  
        while(current != null) {  
            //Prints each node by incrementing pointer  
            System.out.print(current.data + " ");  
            current = current.next;  
        }  
        System.out.println();  
    }  
      
    public static void main(String[] args) {  
          
        SwapNodes sList = new SwapNodes();  
          
        //Add nodes to the list  
        sList.addNode(1);  
        sList.addNode(2);  
        sList.addNode(3);  
        sList.addNode(4);  
        sList.addNode(5);  
          
        System.out.println("Original list: ");  
        sList.display();  
          
        //Swaps the node 2 with node 5  
        sList.swap(2,5);  
          
        System.out.println("List after swapping nodes: ");  
        sList.display();  
    }  
}  

【问题讨论】:

  • 我猜第一个错误是清楚的?您需要重命名源文件以匹配类名,即SwapNodes.java。第二条错误消息指的是源代码中不存在的类SwapNodes.Main。您是否尝试过清理和重建程序?

标签: java


【解决方案1】:

class SwapNodes is public, should be declared in a file named SwapNodes.java 这意味着由于该类是公共的,因此文件名必须是 SwapNodes 而不是 Main。 将文件名更改为SwapNodes。

输出将是:

Original list: 
1 2 3 4 5 
List after swapping nodes: 
1 5 3 4 2 

注意:始终尝试将您提供的文件命名为public 类。 例如:public class ClassName 必须在文件 ClassName.java 中。

【讨论】:

  • 很高兴听到,如果您有公开课,请务必查看您的文件名
【解决方案2】:

从class SwapNodes is public, should be declared in a file named SwapNodes.java 开始,您需要将保存该类的文件重命名为SwapNodes.java,因为顶级类必须位于同名文件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多