Your loop:
i = 0;
while (i < inputs.length) {
palavras.add(inputs[i]);
}
is not incrementing i
, so you will eventually run out of space for the array as you try to insert an infinite number of references to inputs[0]
.
The warning is because you are using a raw ArrayList
. Try declaring palavras
as:
ArrayList<String> palavras = new ArrayList<>(); // new ArrayList<String>(); if pre Java 7
Your other loop:
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++;
}
will generate an IndexOutOfBoundsException
if the first branch of the if
is taken when i == palavras.size() - 1
when it tries to remove the second element. Even if you are guaranteed that "REMOVE"
is always followed by another element, you need to either reverse the order of the removals:
palavras.remove(palavras.get(i + 1));
palavras.remove(palavras.get(i));
or remove the element at i
twice:
palavras.remove(palavras.get(i));
palavras.remove(palavras.get(i));
(because when you call remove()
it moves all succeeding elements down one position in the array).
You are calling t.printTree()
but, there is no such method defined in class Arvore
; the method you did define takes a tree as an argument. Same thing with insert()
: the actual method that's available takes a String
and a No
as arguments.