• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

ArrayListS in an ArrayList that consists of ArrayListS behave like they are the same instance

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am turkish and my variable names are turkish. i hope it won't be a problem.

here is the code:



as you can see there is an arraylist of arraylists of "cumlekelimesi" objects.

i fill the arraylist of arraylist with a templist: "geciciListe". i reconstruct the geciciListe with "new ArrayList<CumleKelimesi>()" at every step.
i can see that lists are filled with the values i wanted, all of them are different.
but when i try to manipulate the arraylists in te arraylist one by one, all the arraylists are effected.

here is manupulation code:



after the line:



denemeKombinasyonuCumleKelimesiListesiListesi[0]
denemeKombinasyonuCumleKelimesiListesiListesi[1]
denemeKombinasyonuCumleKelimesiListesiListesi[1]
denemeKombinasyonuCumleKelimesiListesiListesi[3]

all have become affected.

please pay attention
"it is not problem" of denemeKombinasyonuCumleKelimesiListesiListesi[i] and cka both affected,

"the problem is all the elements of arraylist are affected".

please share what you think.
thanks in advance.
 
Saloon Keeper
Posts: 15714
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merve,

How can you tell they're all affected? Are you testing after the while loop? If so, isn't it logical all the elements would be affected, since you call the method on each of them in the loop?

I would also highly recommend switching to English identifiers. You should always write your code so that other people can understand it, and getting help is a *lot* easier if you use English.
I don't mean to be uppity or anything; I used to write all my identifiers in Dutch. But switching to English will make life a lot more easy for you too.
 
merve klug
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
yes i am debugging and testing after each iteration of the loops.
and arraylists in the arraylist of arraylists are affected even they are the list in the loop in that iteration or not.
 
Stephan van Hulst
Saloon Keeper
Posts: 15714
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please post an SSCCE that demonstrates your problem?
 
merve klug
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
thanks very much for attention
i wll be out for a few hours but i hope i will be see the problem while preparing sscce,
 
Master Rancher
Posts: 5005
79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have no way of knowing what

does. You don't show that code. If the problem shows up immediately after that line, there's a good chance we will want to know what that line does.

Also it would be nice to know what "affected" means here. What effect are you seeing? Why is that a problem? I agree with all Stephan's questions and comments here.
 
merve klug
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
first of all thanks for your time

is it important what Ayristirici.ayristirmaAlgoritmasi(cka) does?

when i want to debug into ayristirmaAlgoritmasi NetBeans show me only the cka and its sub elements.

thus, could the outer arraylist's other element how could be affected?

i am really confused.

in c maybe i can arrive some place out of array but i m in java (

i am going to make a manual debugging like writing system.out.println() between the lines.

thanks again.
 
merve klug
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
problem has been solved.

when i defining a new object with



i was using the constructer



i changed the code with



in a forum it has been told to me to "deep copy" my arraylists
now i see, i must "deep copy" the "member lists" too.

thanks for all your time
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic