Array value get overwrited every time java

109 Views Asked by At

i try to add some objects(servei) in this code and the new one allways overwrite the last one and i can't find the error :/,i have two points p and i,I use i for the array only when deleting llista[p] objects,cause i don't want null values inside my array,i added the code,when i use e,and servei cause some people said that there could be the error too.

System.out.println(llista.afegirServei(serv)); // that's what i use to add a new servei

public class Llistaservei {
    private int i;
    private Servei[] llista;
    private int p = 0;
    private float opinio;

    public Llistaservei(int Max_Serv) {
        llista = new Servei[Max_Serv]; 
    }

    public String AfegirServei(Servei servei) {
        if (ValidServei(servei.getNom()) == i) {
            return ("El servei ja existeix"); //if exist return that "servei" exist.
        } else { ***//finally works cleaning eclipse cache...***
                llista[p]=servei;
            p++;
            return ("El servei s'ha creat correctament");// I add a new "Servei to the array" if doesn't exist
        }
    }

    public String EliminarServei(String nom){
    if(ValidServei(nom)==-1){
        llista[i]=llista[p];    //here i use 2 pointers p,i cause i don't want null 
        llista[p]=null;         //in the middle of the array
        p--;
        return ("El servei s'ha eliminat correctament"); //if is valid 
    }                                                    //return "servei" deleted

    public int ValidServei(String nom) {
        for (i = 0; ((i < p) && (i < llista.length)); i++) {
            if (nom.equalsIgnoreCase(llista[i].getNom())) {
                return i;
            }
        }
    }

}
3

There are 3 best solutions below

2
On

Since ValidServei returns i and shares i your method will always return a value where i matches it here -

if (ValidServei(servei.getNom())==i){

Instead you could write a contains like,

public boolean contains(String nom) {
    for (int i = 0; i < p && i < llista.length; i++) {
        if (nom.equalsIgnoreCase(llista[i].getNom())) {
            return true;
        }
    }
    return false;
}

And Java method names start with a lower case letter by convention. Like,

public String afegirServei(Servei servei) {
    if (contains(servei.getNom())) {
        return ("El servei ja existeix");
    }
    llista[p++] = servei;
    return ("El servei s'ha creat correctament");
}

Finally, you should probably be using a Set<Servei> collection (instead of an array) like,

Set<Servei> set = new HashSet<>();
set.add(servei);
3
On

I can't edit, so here is the translated code. This is only for the community to be able to read your question, since you don't seem to change your code in the question.

public class ServiceList {
private int i;
private Service[] list;
private int p = 0;
private float opinion;

public ServiceList(int maxServer)
{
    list = new Service[maxServer];
}

public String addService(Service service){

    if(chooseService(service.getName()) == i){
        return("The service already exists");
    }
    else
    {
        list[p]=service;
        p++;
        return("The service was successfully created");
    }   

public int chooseService(String name){
    for(i=0; ((i<p) && (i<list.length)); i++){
        if(name.equalsIgnoreCase(list[i].getname())){
            return i;
        }
2
On

I'm guess that, considering the lacks of some } and the value of i in AfegirServei is missing, you're cut your source code and put what you believe that it's necessary to understand the problem. I'm pretty sure that the problem is in the parameter servei when you call public String AfegirServei(Servei servei). It's very possible that you're passing the wrong object or something like that. Of course, I can be sure only I see the full source code.