java - 列表排序程序代码中的静态错误

标签 java static compiler-errors sortedlist

我正在创建一个程序,它从文本文件中获取单词并将所有单词按字典顺序排列,然后还计算频率。它们被分成两列,如下所示:

Word      Occurs
====      ======
a           21
animal       3 
. 
. 
.
zebra        1
====      ======
Total     1325

我尝试编译所有代码并收到此错误:

Count2.java:12: error: non-static variable this cannot be referenced from a 
static context
    List sortedList = new List();

这是我的代码,它有什么问题导致它出现此静态错误?

import java.util.Scanner;

public class Count2
{
    static Scanner stdin = new Scanner(System.in);

    public static void main(String[] args)
    {
    String k = nextWord();
    List sortedList = new List();
    while(k != null)
    {
        sortedList.insert(k);
        k = nextWord();
    }

    sortedList.print();

}

    private static String nextWord()
    {

    if(stdin.hasNext())
    {
        String word = stdin.next();
        word = word.toLowerCase();
        int start = 0;
        int end = word.length();

        for(int c = 0; c < word.length(); ++c)
        {
            if(Character.isLetter(word.charAt(c)) || word.charAt(c) == '-') 
            {
                start = c;
                break;
            }

        for(int n = start; n < word.length(); ++n)
        {
            if(!(Character.isLetter(word.charAt(n)) || word.charAt(n) == '-'))
            {
                end = n;
                break;
            }
        }
        return word.substring(start,end);
    }

    return null;


    } // nextWord


} // end Count2

class List
{
    public class Node
    {
        String word;
        Node next;
        int count;

        public Node(String Words, Node Next)
        {  
            word = Words; 
            next = Next; 
            count = 1; // number of word occurences
        }
    }
    private Node first;
    private int numWords;

    public List() //make an empty list
    { 
        first = null; 
        numWords = 0; 
    }

    public void insert(String word)
    {    
        if(first == null) 
        {
            Node newNode;
            newNode = addNode(word, null);
            first = newNode;          
        }   
        else if(word.equals(first.word)) //first != null, check if word matches first word on List
        {
            first.count++;
        }
        else // first != null and first != first word
        {  
            Node newNode;
            Node current;
            current = first;
            Node previous;
            previous = null;

            int c =  word.compareTo(current.word);

            while ((c > 0) && (current.next != null)) //loop when c is positive
            {
                previous = current;    
                current = current.next;
                c =  word.compareTo(current.word);
            }

            if ((c >0 && current.next == null)) 
            {
                newNode = addNode(word, null);
                current.next = newNode;
            }
            else if (c==0) // increase count when word exists 
            {
                current.count++;
            }
            else 
            { 
                newNode = addNode(word, current);

                if (previous == null) //comes after new word 
                {
                    first = newNode;       
                }         

                else //insert node in middle of list
                {
                    previous.next = newNode;
                }
            }       
        }
    }

    private Node addNode(String word, Node next) //adds new Node and increase counter
    {
        Node newNode = new Node(word, next);
        numWords++;
        return newNode;
    }

    public String[] wordList() 
    {
        String[] WORDS = new String[numWords];
        Node current = first;
        int i =0;

        while (current != null) {     
            WORDS[i] = current.word;
            current = current.next;
            i++;
        }
        return WORDS;
    }   

    public int[] getFrequency() //int array for amount of times a word occurs
    {
        int[] numbers = new int[numWords];
        Node current = first;
        int i =0;

        while (current != null) {
            numbers[i] = current.count;
            current = current.next;
            i++;
        }
        return numbers;
    }

    public void print() // prints words in the list and number of times each word occurs
    {
        int[] numbers = getFrequency();
        String[] WORDS = wordList();

        System.out.println("Word   \t    \t    Occurs");
        System.out.println("====   \t    \t    ======");

        for (int i =0; i < numWords; i++) 
        { 
            System.out.println(WORDS[i] + " \t " + numbers[i] );   
        }
    }      

}
}

最佳答案

两个问题

1.) 方法 nextWord() 应该有一个返回类型。

2.) List SortedList = new List(); 不是处理内部类的有效对象创建。

更改为,

List sortedList = new Count().new List();

关于java - 列表排序程序代码中的静态错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33514383/

相关文章:

c# - 面向对象编程中static修饰符有什么用?

java - 静态成员的多线程行为

c++ - 无法创建 wchar_t 数组

c++ - 错误: variable or field 'functionName' declared void

java - 解析此字符串然后返回它时遇到问题

java - 我在复制二维数组时遇到问题。该数组如下 - {"A", "B"}, {"01", "02", "03"}, {"XX"}};

java - 如何使用带有 Filter 和 FORWARD 调度的 jetty 延续?

java - 使用 java 下载 Blob 会产生一个巨大的文件

java - 以这种方式构造对象是否不符合常规? (关于同一个构造函数的几个问题)

c++ - g++ 编译错误