我调用一个方法,它给了我一个错误

时间:2014-03-16 04:50:42

标签: java methods arraylist warnings

我的源代码:

import java.util.*;

public class Arvore {

    public Arvore() {
        root = null;
    }

    public void inserir(String x) {
        root = insert(x, root);
    }

    public void remove(String x) {
        root = remove(x, root);
    }

    private No remove(String x, No t) {
        if (t == null) {
            return t;
        }

        int comp = x.compareTo(t.str);

        if (comp < 0) {
            t.left = remove(x, t.left);
        } else if (comp > 0) {
            t.right = remove(x, t.right);
        } else if (t.left != null && t.right != null) {
            t.str = findMin(t.right).str;
            t.right = remove(t.str, t.right);
        } else {
            t = (t.left != null) ? t.left : t.right;
        }
        return balance(t);
    }

    public String findMin() {
        return findMin(root).str;
    }

    public String findMax() {
        return findMax(root).str;
    }

    public boolean contains(String x) {
        return contains(x, root);
    }

    public void makeEmpty() {
        root = null;
    }

    public boolean isEmpty() {
        return root == null;
    }
    private static final int maxBal = 1;

    private No balance(No t) {
        if (t == null) {
            return t;
        }

        if (height(t.left) - height(t.right) > maxBal) {
            if (height(t.left.left) >= height(t.left.right)) {
                t = rotateWithLeftChild(t);
            } else {
                t = doubleWithLeftChild(t);
            }
        } else if (height(t.right) - height(t.left) > maxBal) {
            if (height(t.right.right) >= height(t.right.left)) {
                t = rotateWithRightChild(t);
            } else {
                t = doubleWithRightChild(t);
            }
        }

        t.height = Math.max(height(t.left), height(t.right)) + 1;
        return t;
    }

    public void checkBalance() {
        checkBalance(root);
    }

    private int checkBalance(No t) {
        if (t == null) {
            return -1;
        }

        if (t != null) {
            int hl = checkBalance(t.left);
            int hr = checkBalance(t.right);
            if (Math.abs(height(t.left) - height(t.right)) > 1
                    || height(t.left) != hl || height(t.right) != hr) {
                System.out.println("OOPS!!");
            }
        }

        return height(t);
    }

    private No insert(String x, No t) {
        if (t == null) {
            return new No(x, null, null);
        }

        int comp = x.compareTo(t.str);

        if (comp < 0) {
            t.left = insert(x, t.left);
        } else if (comp > 0) {
            t.right = insert(x, t.right);
        } else
            ;

        t.occ+=1;

        return balance(t);
    }

    private No findMin(No t) {
        if (t == null) {
            return t;
        }

        while (t.left != null) {
            t = t.left;
        }
        return t;
    }

    private No findMax(No t) {
        if (t == null) {
            return t;
        }

        while (t.right != null) {
            t = t.right;
        }
        return t;
    }

    private boolean contains(String x, No t) {
        while (t != null) {
            int comp = x.compareTo(t.str);

            if (comp < 0) {
                t = t.left;
            } else if (comp > 0) {
                t = t.right;
            } else {
                return true;
            }
        }

        return false;
    }

    private void printTree(No t) {
        if (t != null) {
            printTree(t.left);
            System.out.println(t.str + ": " + t.occ);
            printTree(t.right);
        }
    }

    private int height(No t) {
        return t == null ? -1 : t.height;
    }

    private No rotateWithLeftChild(No k2) {
        No k1 = k2.left;
        k2.left = k1.right;
        k1.right = k2;
        k2.height = Math.max(height(k2.left), height(k2.right)) + 1;
        k1.height = Math.max(height(k1.left), k2.height) + 1;
        return k1;
    }

    private No rotateWithRightChild(No k1) {
        No k2 = k1.right;
        k1.right = k2.left;
        k2.left = k1;
        k1.height = Math.max(height(k1.left), height(k1.right)) + 1;
        k2.height = Math.max(height(k2.right), k1.height) + 1;
        return k2;
    }

    private No doubleWithLeftChild(No k3) {
        k3.left = rotateWithRightChild(k3.left);
        return rotateWithLeftChild(k3);
    }

    private No doubleWithRightChild(No k1) {
        k1.right = rotateWithLeftChild(k1.right);
        return rotateWithRightChild(k1);
    }


    private class No {

        No(String tStr) {
            this(tStr, null, null);
        }

        No(String tStr, No lt, No rt) {
            str = tStr;
            left = lt;
            right = rt;
            height = 0;
            occ = 1;
        }
        String str;
        No left;
        No right;
        int height;
        int occ = 0;
    }
    private No root;

    public static void main(String[] args) {
        Arvore t = new Arvore();
        System.out.println("AVL TREE TEST\n");
        String msg;
        String[] inputs;
        Scanner sc = new Scanner(System.in);
        ArrayList palavras = new ArrayList();
        int i = 0;

        while (true) {
            msg = sc.nextLine();
            if (msg.equals("")) {
                break;
            }


            inputs = msg.split(" ");

            i = 0;

            while (i < inputs.length) {
                palavras.add(inputs[i]);
            }

        }

        i = 0;
        while (i < palavras.size()) {
            if (palavras.get(i).equals("REMOVE")) {
                palavras.remove(palavras.get(i));
                palavras.remove(palavras.get(i + 1));
                i += 2;
            } else {
                t.insert(palavras.get(i));
            }
            i++;
        }




    t.printTree();
    }
}

在主函数上调用insert和printTree时,我无法弄清楚为什么会出现错误。 当我做palavras.add(inputs[i])时,我会通过向我的arraylist添加一个字符串来发出警告。

1 个答案:

答案 0 :(得分:0)

你的循环:

i = 0;

while (i < inputs.length) {
    palavras.add(inputs[i]);
}

没有递增i,因此当您尝试向inputs[0]插入无限数量的引用时,最终会耗尽数组的空间。

警告是因为您使用的是原始ArrayList。尝试将palavras声明为:

ArrayList<String> palavras = new ArrayList<>(); // new ArrayList<String>(); if pre Java 7

你的另一个循环:

i = 0;
while (i < palavras.size()) {
    if (palavras.get(i).equals("REMOVE")) {
        palavras.remove(palavras.get(i));
        palavras.remove(palavras.get(i + 1));
        i += 2;
    } else {
        t.insert(palavras.get(i));
    }
    i++;
}
如果在IndexOutOfBoundsException尝试删除第二个元素时if采用i == palavras.size() - 1的第一个分支,

将生成"REMOVE"。即使您保证palavras.remove(palavras.get(i + 1)); palavras.remove(palavras.get(i)); 始终跟随另一个元素,您也需要反转删除的顺序:

i

或删除palavras.remove(palavras.get(i)); palavras.remove(palavras.get(i)); 两次的元素:

remove()

(因为当你调用t.printTree()时,它会将所有后续元素向下移动到数组中的一个位置。)

您正在调用Arvore但是,类insert()中没有定义此类方法;您定义的方法将树作为参数。与String相同的是:可用的实际方法需要No和{{1}}作为参数。