Search code examples
javalistfor-looparraylistaddition

How to add an element to a list in Java?


I'm not a english native. If you found any error , please correct me. I am creating an algo which maniplulates lists. But i have a problem in one of my method. Here it is:

private List<Terme[]> pairing(List<Terme> right, List<Terme> left) {
        List<Terme[]> res = new ArrayList<Terme[]>();
        int i = 0;
        
        for(Terme element : right) {
            tampon[0] = element;
            tampon[1] = left.get(i);
            System.out.println("liste de tampon["+i+"]: "+tampon[0].getNomC()+", "+tampon[1].getNomC());
            res.add(tampon);
            this.printPair(res);
            i++;
        }
        
        return res;
    }

I have add the println to find the problem. Here what it prints:

P(x,x,f(t)), P(y,a,u)
liste de tampon[0]: x, y
Pairing: {{x,y}}
liste de tampon[1]: x, a
Pairing: {{x,a},{x,a}}
liste de tampon[2]: f(t), u
Pairing: {{f(t),u},{f(t),u},{f(t),u}}

This method is used in this method of class:

public void setDiff(Predicat[] couple) {
        List<Terme[]> liste = new ArrayList<Terme[]>();
        List<Terme[]> res = new ArrayList<Terme[]>();
        liste = this.pairing(couple[0].getPredicatDe(), couple[1].getPredicatDe());
        this.printPair(liste);
        boolean simple;
        
        do{
            res = new ArrayList<Terme[]>();
            for(Terme[] element : liste) {
                if(element[0].isEquals(element[1]) == false) {
                    res.add(element);
                }else if((element[0].isType("Fonction")==true) && (element[1].isType("Fonction")==true)) {
                    for(Terme[] litt : this.pairing(((Fonction) element[0]).getFonctionDe(), ((Fonction) element[1]).getFonctionDe())) {
                        res.add(litt);
                    }
                }
                System.out.println(
            }
            liste = res;
            diff = liste;
            this.printDiff();
            simple = this.isSimple();
        }while(simple == false);
        diff = liste;
    }

Here what it shows when called:

P(x,x,f(t)), P(y,a,u)
liste de tampon[0]: x, y
Pairing: {{x,y}}
liste de tampon[1]: x, a
Pairing: {{x,a},{x,a}}
liste de tampon[2]: f(t), u
Pairing: {{f(t),u},{f(t),u},{f(t),u}}
Pairing: {{f(t),u},{f(t),u},{f(t),u}}
Diff: {{f(t),u},{f(t),u},{f(t),u}}

What I want is the following results:

Pairing: {{x,y},{x,a},{f(t),u}}
Diff: {{x,y},{x,a},{f(t),u}}

For those who are curious about the program i'm writing just tell me how to share it.

Thanks in advance.


Solution

  • You update a single object tampon of type Terme[]. That means that the list will end up with several references to that same object. What you need to do is create a new array inside the loop, and add that:

    for(Terme element : right) {
        Terme[] newElement = { element, left.get(i), };
        System.out.println("liste de newElement["+i+"]: "+newElement[0].getNomC()+", "+newElement[1].getNomC());
        res.add(newElement);
        this.printPair(res);
        i++;
    }
    

    If the tampon field is now unused, you can remove it, and rename my newElement to tampon again.