Array ordenção BoobleSort
Bom dia, estou tentando implementar o BoobleSort em um Interator mas está ocorrendo um erro que não consigo identificar qual é, alguém poderia me ajudar, é um trabalho de faculdade, o erro ocorre no if:
for (int i = array.size() - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (array.get(j) > array.get(j + 1)) {
Aluno aux = array.get(j);
array.set(j, array.get(j + 1));
array.set((j + 1), aux);
}
}
}
Roberval
Curtidas 0
Respostas
Jothaz
10/09/2015
Qual erro retorna?
GOSTEI 0
Roberval
10/09/2015
"bad operand types for binary operator".
GOSTEI 0
Jothaz
10/09/2015
Posta o conteúdo do array.
GOSTEI 0
Roberval
10/09/2015
ArrayList<Professor> array = new ArrayList<Professor>();
FileReader fr = new FileReader(nomeDoArquivo);
BufferedReader br = new BufferedReader(fr);
String linha = "";
int pos = 0;
while ((linha = br.readLine()) != null) {
Professor aux = new Professor(linha);
array.add(aux);
pos++;
}
br.close();
for (int i = array.size() - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (array.get(j) > array.get(j + 1)) {
Aluno aux = array.get(j);
array.set(j, array.get(j + 1));
array.set((j + 1), aux);
}
}
}
return array.iterator();
GOSTEI 0
Jothaz
10/09/2015
Qual o conteúdo do array: numérico,alfa, alfanumérico?
GOSTEI 0
Roberval
10/09/2015
Alfanumérico.
GOSTEI 0
Jothaz
10/09/2015
Olha acho bem mais simples usar:
Troque isto:
`Por isto:
Arrays.sort(strArr);
Troque isto:
if (array.get(j) > array.get(j + 1)) {
`Por isto:
if(array[j].compareTo(array[j+1])>0) {
GOSTEI 0
Roberval
10/09/2015
Deu certo aqui, obrigado.
GOSTEI 0