Pessoal… tenho uma classe que deve fazer o seguinte:
Tem um arraylist que recebe uma frase, mostra se alguma palavra da frase tem o “radical” _N junto dela, se tiver mostra a posição que ela está, mas depois de mostrar a posição dela não consigo mostrar o seu conteúdo.
package calculos;
import java.util.ArrayList;
public class Corpus {
Corpus() {
[color=green] //Armazenar todos os _N em uma lista[/color]
ArrayList<Object> al = new ArrayList<Object>();
al.add("teste_N um_O array_P list_N para_I extrair_O valores_U");
Iterator it = al.iterator();
int index;
String str;
for (int i = 0; i < al.size(); i++) {
str = (String)it.next();
index = str.indexOf("_N");
if (al.contains(str)){
System.out.print("Sim, existe pelo menos um _N \n\n");
}
System.out.print("Se existe um _N, em que posição ele está? \n\n");
if (index > -1) {
System.out.println("Está na posição: " + index);
i = al.size();
String tst = (String) al.get(index);
System.out.println("Está na posição: " + index);
}
}
}
}
[color=red]Saída:[/color]
Sim, existe pelo menos um _N
Se existe um _N, em que posição ele está?
Está na posição: 5
Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 5, Size: 1
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at calculos.Corpus.(Corpus.java:39)
at calculos.CPrincipal.main(CPrincipal.java:8)
Obrigado,
Abraços