Ciao a tutti,
il mio problema è dato dall'errore di stack overflow durante l'ordinamento alfabetico di alcuni elementi tramite insertion sort.
questo è il codice di confronto che ho usato per le stringhe:
FUNZIONE "MAGGIORE DI":
FUNZIONE "MINORE DI":codice:public boolean maggiore(Elemento e,int ch) { if(ch<this.nome.length()) { if(ch==e.nome().length()) { return true; } if(this.nome.charAt(ch)>e.nome.charAt(ch)) { return true; } else if(this.nome.charAt(ch)==e.nome.charAt(ch)) { return maggiore(e,ch++); } else if(this.nome.charAt(ch)<e.nome.charAt(ch)) { return false; } } return false; }
E QUESTO E' L'INSERTION SORT CHE HO CREATO:codice:public boolean minore(Elemento e,int ch) { if(ch<this.nome.length()) { if(ch==e.nome().length()) { return true; } if(this.nome.charAt(ch)<e.nome.charAt(ch)) { return true; } else if(this.nome.charAt(ch)==e.nome.charAt(ch)) { return maggiore(e,ch++); } else if(this.nome.charAt(ch)>e.nome.charAt(ch)) { return false; } } return false; }
Insertion sort crescente
Insertion sort Decrescentecodice:public void insertionSortCrescente(Vector<Elemento> elev, int min, int max,int categoria) { for(int i = min+1; i < max; i++) { int x = i; int j = i-1; for(; j >= min; j--) { if(elev.elementAt(j).maggiore(elev.elementAt(x),0)) { Elemento p = elev.elementAt(x); elev.removeElementAt(x); elev.add(x,elev.elementAt(j)); elev.removeElementAt(j); elev.add(j,p); x = j; } else { break; } } } ZemyList.l.categorie.elementAt(categoria).Rb().removeAllElements(); for(int z=0;z<elev.size();z++) { ZemyList.l.categorie.elementAt(categoria).Rb().add(new JCheckBox(elev.elementAt(z).nome())); // System.out.println(ZemyList.l.categorie.elementAt(categoria).Rb().elementAt(z).getName()); } }
Come posso migliorare la logica di ordinamento affinché non insorga lo Stack Overflow Error?codice:public void insertionSortDecrescente(Vector<Elemento> elev, int min, int max,int categoria) { for(int i = min+1; i < max; i++) { int x = i; int j = i-1; for(; j >= min; j--) { if(elev.elementAt(j).minore(elev.elementAt(x),0)) { Elemento p = elev.elementAt(x); elev.removeElementAt(x); elev.add(x,elev.elementAt(j)); elev.removeElementAt(j); elev.add(j,p); x = j; } else { break; } } } ZemyList.l.categorie.elementAt(categoria).Rb().removeAllElements(); for(int z=0;z<elev.size();z++) { ZemyList.l.categorie.elementAt(categoria).Rb().add(new JCheckBox(elev.elementAt(z).nome())); // System.out.println(ZemyList.l.categorie.elementAt(categoria).Rb().elementAt(z).getName()); } }
Grazie a tutti =)

Rispondi quotando