1) visto che negli if hai un return non ti serve usare il costrutto else if...ti bastano solo tanti if perchè, non appena uno si verifica il metodo restituisce un valore "terminando" la sua esecuzionecodice:public int add(int id, String word){ int n = str.length; int statment = 0; for(int i = 0; i < n; i++){ if(str[i].getID() == id){ System.out.println("duplicate found"); return statment = KO_DUPLICATE_FOUND; } else if(word == ""){ System.out.println("null string"); return statment = KO_NULL_STRING; } else if(str[i] == null && i < n){ System.out.println("full pool"); return statment = KO_FULL_POOL; } else if(id < 0 ){ System.out.println("id non valido"); return statment = ID_NOT_VALID; } else{ str[i] = new StringUtils(id,word); System.out.println("STRING_POOL_SUCCES Code Return : " + STRING_POOL_SUCCESS); return statment = STRING_POOL_SUCCESS;} } return statment; }
2) immagino che tu ti stia costruendo una struttura dati e il metodo add lo invochi quando aggiungi un elemento a tale strutura....quindi l'if(word == "") non ha senso metterlo nel ciclo for in quanto così verà controllato n volte ma a te basta che venga controllato una sola volta
3) if(str[i] == null && i < n) non mi è chiaro...se vuoi sapere se il contenitore è pieno dovresti verificare che str[i] NON sia null e, soprattuto la condizione i<n è sempre vera nel ciclo for quindi non ha senso aggiungerla....ti dirà di più...la tua struttura dati, per evitare questo controllo, io la cambierei aggiungendo un contatore che aumenta e decresce in base all'inserimento, cancellazione di un oggetto dalla struttura...in questo modo, quando invochi .add() ti basta controllare che contatoreElementi != n evitando così n-1 if
4) if(id < 0 ) è come il punto 2. Dovrebbe stare fuori dal for
detto ciò, ammesso che non mi sfugga nulla, come modificheresti il metodo add della tua struttura dati?

Rispondi quotando