Silly trouble

Hi friends i've a newbie's trouble.....

i've this simply code to create a disposition on n variables....

publicclass Main{

staticint posiz = 0;

static ArrayList app_arr;

static ArrayList dispo;

staticint numsol = 0;

publicstaticvoid main(String[] args){

app_arr =new ArrayList();

for (int i = 0; i < 10; i++){

app_arr.add(i, 0);

}

dispo =new ArrayList();

disposition(10);

ArrayList app2 =new ArrayList();

for (int i = 0; i < dispo.size(); i++){

System.out.println(dispo.get(i));

}

}

publicstaticvoid disposition(int length){

int bit = 0;

do{

app_arr.set(posiz, bit);

posiz++;

if(posiz==length){

//System.out.print(app_arr);

dispo.add(numsol, app_arr);

numsol++;

System.out.println("Disposizione numero "+numsol);

}

else{

disposition(length);

}

posiz--;

bit++;

}

while(bit<2);

}

}

but when i print the dispo ArrayList there is always the same (the last) disposition!!....why? how i can solve this problem?...thanks

[2543 byte] By [marlborinoa] at [2007-11-27 1:55:38]
# 1
There's anyone who can help me?....i need for a method which creates all the disposition of n (parameter of the method) bit and it storages into a ArrayList or a Vector and all these structures are storaged into only one Vector or ArrayList....thanks
marlborinoa at 2007-7-12 1:28:51 > top of Java-index,Java Essentials,Java Programming...
# 2

I don't have my Italian dictionary at hand. My English dictionary translates "disposition" as "(natural) bent", "inclination" or "arrangement", neither of which is precise enough for me to make sense of what you're saying. So please explain more clearly.

You're code looks confusing to me, especially the recursion controlled by a global variable (posiz). You may consider starting over with clearer code.

OleVVa at 2007-7-12 1:28:51 > top of Java-index,Java Essentials,Java Programming...
# 3

I don't know the correct translation but i can make you a example.....

If i want the disposition of 3 bit variables the result will be:

000

001

010

011

100

101

110

111

This code is quite correct because it creates well the "dispositions" (sorry but i don't know the real name in english!) into arr_app but it doesn't save well them into the second ArrayList dispo.

It puts always the last disposition which is founded!!

marlborinoa at 2007-7-12 1:28:51 > top of Java-index,Java Essentials,Java Programming...
# 4

Now it's clear to me what you want to do.

I'm still having trouble reading your code. However, I agree that recursion is warranted.

Could it be this?

dispo.add(numsol, app_arr);

You add the app_arr ArrayList as an element to your dispo ArrayList. However, you add the same ArrayList object every time, so dispo ends up holding several references to the same ArrayList. This would explain why every entry seems to hold the last disposition created (let's just call them dispositions until someone may propose a better term; it seems to agree with the translation as "arrangement").

Instead you need to add a copy of ap_arr to dispo.

Message was edited by:

OleVV

OleVVa at 2007-7-12 1:28:51 > top of Java-index,Java Essentials,Java Programming...
# 5

dispo = new ArrayList(); // now dispo points to another (empty) ArrayList - original list is gone

disposition(10);// method call adds stuff to the new (empty) ArrayList

If you want to keep adding elements to dispo, don't instantiate a new one each time.

static ArrayList dispo = new ArrayList(); // created just once at class level

As for app_arr, you're creating a new one each time, and filling it with the same elements. ..? Use a static initializer.

karma-9a at 2007-7-12 1:28:51 > top of Java-index,Java Essentials,Java Programming...