0

my arraylistS in a covering arraylist behave like the same instance.

i manupulate one of them with

i=0; manupulate((ArrayList)theCoveringRootArrayList.get(i));

, and all the sub arraylists have become effected of the manupulation.

i did my homework i googled a little but in google it was said: "create new instance with myvar=new myobject()"

i have already done so.

here is the code:

    ArrayList denemeKombinasyonuCumleKelimesiListesiListesi=new ArrayList();

    int i=0,j=0;
    ArrayList<CumleKelimesi> geciciListe=new ArrayList<CumleKelimesi>();
    while(matris[0][j]!=-1){
        geciciListe=new ArrayList<CumleKelimesi>();
        i=0;
        while(i<cumle.cumleKelimeleri.size()){
            if(!cumle.cumleKelimeleri.get(i).noktalamaMi){
            geciciListe.add(cumle.cumleKelimeleri.get(i).olasilikliKelimeler.get(matris[i][j]));
            }else{
            geciciListe.add(cumle.cumleKelimeleri.get(i));
            }
            i++;
        }
        denemeKombinasyonuCumleKelimesiListesiListesi.add(geciciListe);
        j++;

    }
    i=0;
    int enAzAyristirilamayan=9999;
    int enAzAyristirilamayanliListeninYeri=0;
    while(i<denemeKombinasyonuCumleKelimesiListesiListesi.size()){
        ArrayList<CumleKelimesi> cka=new ArrayList<CumleKelimesi>();
        cka.addAll((ArrayList)denemeKombinasyonuCumleKelimesiListesiListesi.get(i));
        int ayristirilamayanSayisi=Ayristirici.ayristirmaAlgoritmasi(cka);
            if(ayristirilamayanSayisi<enAzAyristirilamayan){
                enAzAyristirilamayan=ayristirilamayanSayisi;
                enAzAyristirilamayanliListeninYeri=i;
            }
         denemeKombinasyonuCumleKelimesiListesiListesi.set(i, cka);
        i++;
    }
    geciciListe=(ArrayList)denemeKombinasyonuCumleKelimesiListesiListesi.get(enAzAyristirilamayanliListeninYeri);

    return geciciListe;

as you can see i do everything to make feel them they are different instances but, every one is effected with manupulate method. my variables name are in turkish but i think you can see what is what

for example

geciciliste=a temp list

denemeKombinasyonuCumleKelimesiListesiListesi=the encapsulating list as arraylist of arraylists what can be the problem thanks in advance


----------------edit---------------------------------------------------------------------

here is my tryings for deep copy task:

    ArrayList<ArrayList<CumleKelimesi>> denemeKombinasyonuCumleKelimesiListesiListesi=new       ArrayList<ArrayList<CumleKelimesi>>();

    int i=0,j=0;
    ArrayList<CumleKelimesi> geciciListe=new ArrayList<CumleKelimesi>();
    while(matris[0][j]!=-1){
        geciciListe=new ArrayList<CumleKelimesi>();
        i=0;
        while(i<cumle.cumleKelimeleri.size()){
            if(!cumle.cumleKelimeleri.get(i).noktalamaMi){
            geciciListe.add(cumle.cumleKelimeleri.get(i).olasilikliKelimeler.get(matris[i][j]));
            }else{
            geciciListe.add(cumle.cumleKelimeleri.get(i));
            }
            i++;
        }
        denemeKombinasyonuCumleKelimesiListesiListesi.add(geciciListe);
        j++;

    }
    i=0;
    int enAzAyristirilamayan=9999;
    int enAzAyristirilamayanliListeninYeri=0;
    while(i<denemeKombinasyonuCumleKelimesiListesiListesi.size()){
        ArrayList<CumleKelimesi> cka=new ArrayList<CumleKelimesi>();
        MetindenGrafOlusturma.copy(denemeKombinasyonuCumleKelimesiListesiListesi.get(i),cka);
        int ayristirilamayanSayisi=Ayristirici.ayristirmaAlgoritmasi(cka);
            if(ayristirilamayanSayisi<enAzAyristirilamayan){
                enAzAyristirilamayan=ayristirilamayanSayisi;
                enAzAyristirilamayanliListeninYeri=i;
            }
         denemeKombinasyonuCumleKelimesiListesiListesi.set(i, cka);
        i++;
    }
    geciciListe=(ArrayList)denemeKombinasyonuCumleKelimesiListesiListesi.get(enAzAyristirilamayanliListeninYeri);

    return geciciListe;

}

public static void copy(ArrayList theList,ArrayList deepCopy) { //deepCopy = new ArrayList();//if i un comment deepcopy would be empty for (CumleKelimesi ck : theList) deepCopy.add(ck.clone()); }

here is the CumleKelimesi object s clone method

     public CumleKelimesi clone()
      {
          try
      {
              return (CumleKelimesi)super.clone();
          }
      catch( CloneNotSupportedException e )
      {
              return null;
          }
      }

if all these are wrong please advise a different deep copy method and i am very confused

merve
  • 1
  • 1
  • 1
    are you struggling to make a [deep copy](http://stackoverflow.com/questions/715650/how-to-clone-arraylist-and-also-clone-its-contents)? – mre Jun 06 '11 at 12:03
  • 1
    None of this is your *real* code; there are too many little error messages and the variable names aren't realistic. It's extremely likely that there's something about your real code which is wrong, but not reflected in this summary. – Ernest Friedman-Hill Jun 06 '11 at 12:09
  • hi thanks for advise i edit all the code but var names are in turkish, hope it wont be a problem to answer – merve Jun 06 '11 at 12:14

3 Answers3

3

You are making a shallow copy of the list when you say: a=(ArrayList)theCoveringRootArraylist.get(i);

The ith index in theCoveringRootArraylist and the list variable 'a' point to the same list and hence both are modified when you "manupulate(a)".

Try making a deep copy and then change.

Anshul
  • 806
  • 1
  • 7
  • 9
  • i am going to make a deep copy but will be away for few hours. thanks very much all your help – merve Jun 06 '11 at 12:49
0

Here is how I tried to deep copy task:

    ArrayList<ArrayList<CumleKelimesi>> denemeKombinasyonuCumleKelimesiListesiListesi=new       ArrayList<ArrayList<CumleKelimesi>>();

    int i=0,j=0;
    ArrayList<CumleKelimesi> geciciListe=new ArrayList<CumleKelimesi>();
    while(matris[0][j]!=-1){
        geciciListe=new ArrayList<CumleKelimesi>();
        i=0;
        while(i<cumle.cumleKelimeleri.size()){
            if(!cumle.cumleKelimeleri.get(i).noktalamaMi){
            geciciListe.add(cumle.cumleKelimeleri.get(i).olasilikliKelimeler.get(matris[i][j]));
            }else{
            geciciListe.add(cumle.cumleKelimeleri.get(i));
            }
            i++;
        }
        denemeKombinasyonuCumleKelimesiListesiListesi.add(geciciListe);
        j++;

    }
    i=0;
    int enAzAyristirilamayan=9999;
    int enAzAyristirilamayanliListeninYeri=0;
    while(i<denemeKombinasyonuCumleKelimesiListesiListesi.size()){
        ArrayList<CumleKelimesi> cka=new ArrayList<CumleKelimesi>();
        MetindenGrafOlusturma.copy(denemeKombinasyonuCumleKelimesiListesiListesi.get(i),cka);
        int ayristirilamayanSayisi=Ayristirici.ayristirmaAlgoritmasi(cka);
            if(ayristirilamayanSayisi<enAzAyristirilamayan){
                enAzAyristirilamayan=ayristirilamayanSayisi;
                enAzAyristirilamayanliListeninYeri=i;
            }
         denemeKombinasyonuCumleKelimesiListesiListesi.set(i, cka);
        i++;
    }
    geciciListe=(ArrayList)denemeKombinasyonuCumleKelimesiListesiListesi.get(enAzAyristirilamayanliListeninYeri);

    return geciciListe;

}

public static void copy(ArrayList<CumleKelimesi> theList,ArrayList<CumleKelimesi> deepCopy) {
    //deepCopy = new ArrayList<CumleKelimesi>();//if i un comment deepcopy would be empty
    for (CumleKelimesi ck : theList)
        deepCopy.add(ck.clone());
}

Here is the CumleKelimesi object's clone method:

     public CumleKelimesi clone()
      {
          try
      {
              return (CumleKelimesi)super.clone();
          }
      catch( CloneNotSupportedException e )
      {
              return null;
          }
      }

If all these are wrong please advise a different deep copy method, thanks.

DarthJDG
  • 16,511
  • 11
  • 49
  • 56
merve
  • 1
  • 1
0

When defining a new object with

new MyObj(myObj);

I was using the constructor

public MyObj(MyObj myObj){
this.memberlist=myObj.memberlist;
//the mistake was here 
//memberlist is another arraylist
//must deep copy this too
...

}

I changed the code with

public MyObj(MyObj myObj){
this.memberlist=new ArrayList<AnotherObject>();
this.memberlist.addAll(myObj.memberlist);
...

}

Here it has told to me to "deep copy" my arraylists. Now I see that I must "deep copy" the "member lists", too.

zondo
  • 19,901
  • 8
  • 44
  • 83
merve
  • 1
  • 1