找不到Java主类

时间:2014-09-22 07:08:28

标签: java singly-linked-list

您好,这是第一次使用本网站回答任何问题,所以轻松搞定格式错误,英语也是我的第二语言。因此,对于我的计算机科学课,我们被要求设计一个字符串的链接列表来创建一个单词,如字母,我没有在制作课程时遇到麻烦,除非在任何IDE上运行它根本不能找到主要班级。我还测试了一个不同的简单hello world项目,看看我的IDE是否正常工作,hello world是否正常工作。如果有人发现我的编码有任何问题,请随时给我反馈。

SinglyLinkedListInterface:

public interface SinglyLinkedListInterface<String>
{
    public void print ( );
    public void traversal ( );
    public Node getNext ( );
    public void addStringToFront (String newItem);
    public void addStringToRear (String newItem);
    public void addStringToMiddle (Node pos, String newItem);
    public String deleteFromFront ( );
    public String deleteFromRear ( );
    public String deleteFromMiddle (Node pos);
    public boolean isEmpty ( );
}

节点:

public class Node<String>
{
    public String data;
    public Node link;

    public Node ( )
    {
        data = null;
        link = null;
    }

    public Node (String newItem)
    {
        data = newItem;
        link = null;
    }

    public void setData (String newData)
    {
        data = newData;
    }

    public void setLink (Node newLink)
    {
        link = newLink;
    }

    public String getData ( )
    {
        return data;
    }

    public Node getLink ( )
    {
        return link;
    }

    public java.lang.String toString ( )
    {
        return data.toString ( );
    }

}

SinglyLinkedList:

public class SinglyLinkedList<String> implements SinglyLinkedListInterface<String>
{
    private Node head;
    private Node cursor;

    public SinglyLinkedList ( )
    {
        head = null;
        cursor = null;
    }

    public boolean isEmpty ( )
    {
        return head == null;
    }

    public Node getNext ( )
    {
        if (isEmpty ( ))
            return null;
        else
        {
            if (cursor.getLink ( ) == null)
                return null;
            else
            {
                cursor = cursor.getLink ( );
                return cursor;
            }
        }
    }

    private void resetCursor ( )
    {
        cursor = head;
    }

    public void traversal ( )
    {
        if (!isEmpty ( ))
        {
            resetCursor ( );

            System.out.println (cursor);
            while (getNext ( ) != null)
                System.out.println (cursor);
        }
    }

    public void print ( )
    {
        traversal ( );
    }

    public void addStringToFront (String newItem)
    {
        Node newNode = new Node (newItem);
        newNode.setLink (head);
        head = newNode;    
    }

    private Node moveStringToRear ( )
    {
        if (cursor != null) //do it only when the list is not empty
        {
            while (getNext ( )!= null);
        }
        return cursor;
    }

    public void addStringToRear (String newItem)
    {
        if (isEmpty ( ))
            addStringToFront (newItem);
        else
        {
            Node newNode = new Node (newItem);
            moveStringToRear ( );
            cursor.setLink (newNode);
        }
    }

    private Node backStringrack ( )
    {
        if (cursor != head)
        {
            Node temp = head;
            while (temp.getLink ( ) != cursor)
                temp = temp.getLink ( );
            cursor = temp;
            return cursor;
        }
        else
            return null;
    }

    //for sorted linked list
    public void addStringToMiddle (Node pos,String newItem)
    {
        Node newNode = new Node (newItem);
        cursor = pos;
        backStringrack ( );
        newNode.setLink (pos);
        cursor.setLink (newNode);
    }

    public String deleteFromFront ( )
    {
        String deleted = null;
        if (!isEmpty ( ))
        {
            resetCursor ( );
            deleted = (String) cursor.getData ( );
            head = head.getLink ( );
            resetCursor ( );                  
        }
        return deleted;
    }

    public String deleteFromRear ( )
    {
        String deleted = null;
        if (!isEmpty ( ))
        {
            moveStringToRear ( );
            deleted = (String) cursor.getData ( );
            if (backStringrack ( ) != null)
                cursor.setLink (null);
            else   //the deleted node at rear is the only node in the list
                head = cursor = null; 
        }
        return deleted;
    }

    //for sorted linked list
    public String deleteFromMiddle (Node pos)
    {
        cursor = pos;
        backStringrack ( );
        cursor.setLink (pos.getLink ( ));
        return (String)pos.getData ( ); //corrected 09172014

    }

    public Node Search1(String key)
    { while(getNext()!= null)
        {
                  if(key.equals(getNext().getData()))
                  {
                      cursor.setLink(head);
                  }


        }

    return cursor ; 
    }



}

主:

public class String 
{
        public static void main (String [] args)
    {        
        SinglyLinkedList x = new SinglyLinkedList();
        x.addStringToFront("s");
        x.addStringToFront("r");
        x.addStringToFront("e");
        x.addStringToFront("t");
        x.addStringToFront("t");
        x.addStringToFront("e");
        x.addStringToFront("l");
        x.print();






    }

}

4 个答案:

答案 0 :(得分:3)

您不应该致电主要课程String

此外,大多数 NoClassDefFound/ClassNotFound只需运行一个干净的构建即可解决。

修改

Java通过FQN(完全限定的类名)引用类名,但是内置的语言结构会被自动识别(例如String - &gt; java.lang.String

如果你需要调用你的类String,你需要通过他们的FQN来引用所有基于语言(java)的实例(假设你的String类对你的所有类都是可见的,即在类路径)

答案 1 :(得分:2)

将类的名称更改为更符合您需要的字符串。 String类是一个内置的Java类。

答案 2 :(得分:2)

你有2个选项可以将你的类重命名为String或者如果你真的想要使用String作为你的类名,那么,在main方法声明中,使用正确的包名称前缀String,因为java的主要方法需要类型为java.lang.String

的数组
public class String
{
    public static void main (java.lang.String [] args)
    {


        System.out.println("ran");


    }

}

答案 3 :(得分:0)

有一个班级&#39;字符串&#39;在java.lang包中定义,它被隐式导入。因此,当您使用库提供的String或者将类的名称更改为其他名称时,您应该使用完全限定名称。