开发者

arraylistS in the arraylist of arraylistS behave like the same instance despite of "new Arraylist<MyObj>()"

开发者 https://www.devze.com 2023-03-10 22:29 出处:网络
my arraylistS in a covering arraylist behave like the same instance. i manupulate one of them with i=0; manupulate((ArrayList)theCoveringRootArrayList.get(i));

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 denemeKombinasyonuCumleKelimesiListesiListes开发者_如何学Ci=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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消