java - JAVA数组索引越界异常

标签 java arrays exception

我尝试运行这段代码,它是一个移到前面的编码器,但我得到了这个异常:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at MTFencoder.main(MTFencoder.java:10)

有人能解释一下为什么会发生这种情况吗?

import java.io.FileReader;
import java.io.BufferedReader;
import java.util.StringTokenizer;
import java.io.IOException;

public class MTFencoder {
    public static void main(String[] args) {
        String filename = args[0];
        WordList wordList = new WordList();

        try {
            String line;
            BufferedReader br = new BufferedReader(new FileReader(filename));
            StringTokenizer st;
            int index;

            while ((line = br.readLine()) != null) {
                st = new StringTokenizer(line);

                while (st.hasMoreTokens()) {
                    String currentword = st.nextToken();

                    if (wordList.hasElement(currentword)) {
                        index = wordList.Index(currentword);
                        wordList.remove(currentword);
                        wordList.add(currentword);
                        System.out.println(Integer.toString(index));
                    } else {
                        wordList.add(currentword);
                        System.out.println("0" + currentword);
                    }
                }
            }
        } catch (IOException e) {
        }
    }
}


class Node {
    private String word;
    private Node next;

    public Node(String w) {
        word = w;
        next = null;
    }

    public String getWord() {
        return word;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node n) {
        next = n;
    }
}


class WordList {
    Node head;

    public WordList() {
    }

    public void add(String w) {
        Node n = new Node(w);
        n.setNext(head);
        head = n;
    }

    public int length() {
        Node tmp = head;
        int count = 0;
        while (tmp != null) {
            count++;
            tmp = tmp.getNext();
        }
        return count;
    }

    public boolean hasElement(String w) {
        Node tmp = head;
        while (tmp != null) {
            if (tmp.getWord().equals(w)) return true;
            tmp = tmp.getNext();
        }
        return false;
    }

    public void remove(String w) {
        if (!hasElement(w)) return;
        if (head.getWord().equals(w)) {
            head = head.getNext();
            return;
        }
        Node tmp = head;
        while (!(tmp.getNext().getWord().equals(w))) tmp = tmp.getNext();
        tmp.setNext(tmp.getNext().getNext());
    }

    public void dumpList() {
        for (Node tmp = head; tmp != null; tmp = tmp.getNext())
            System.out.println(" >> " + tmp.getWord());
    }

    public int Index(String w) {
        int counter = 1;
        Node temp = head;
        while (!(temp.getWord().equals(w))) {
            counter++;
            temp = temp.getNext();
        }
        return counter++;
    }
}

最佳答案

程序需要命令行参数。你没有通过。

该程序需要作为java MTFencoder fileyouwanttoprocess运行。您没有传递文件名参数。

这是第 10 行

String filename = args[0];

args[0] 是第一个命令行参数 - 您没有传递命令行参数。

关于java - JAVA数组索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15737788/

相关文章:

java - 在java中使用内部类扩展类(并且从一个类到另一个类时也扩展内部类)

java - 带有 java.lang.outofmemoryerror 的 React-native run-android 命令错误

java - ElasticSearch 5.4.1 Java API : Error in updateResponse. getResult() 为空,

java - 查找数组中最大的平均结果集

c# - 异常后自动继续

java - 使用 SAS 从 Java 批量插入 Azure 表失败

java - 为什么我得到的是数组的地址而不是值(二维数组)

c - 2 个关于 typedef 结构和平均成绩的问题。我在 C 中做得正确吗?

vb.net - 仅在发布时捕获异常

exception - ARM 处理器上的数据类型未对齐异常(0xA0000002)