array of Object point to the same Obj!
Hi,
I created an object Cell (very simple: it is an int array of length 2), then another object called CellVector which is an array of Cell and contained some methods for updates.
Now I tell you what operations I do:
CellVector c = new CellVector(size); // size=length of the array of Cell
for(int i=0;i<size;i++)
c.addCell();
Here is the method addCell:
public void addCell(int i){
Cell toadd = new Cell(i);
cells[pos] = toadd;
pos++;
}
Then I read a list binary strings, each Cell of the CellVector takes note of how many 1s and hoe many 0s are contained in each string.
// myCell is the CellVector, "cells" is the array of Cell it contains
while((line=in.readLine())!=null){
for(int i=0;i<size;i++){
bit=line.charAt(i);
if(bit=='0') myCells.cells.addZero();
else myCells.cells.addOne();
}
}
The result is that ALL the Cells of the array in CellVector are updated each time for each binary string, thus it seems that actually all the positions of the array point to the same object Cell, I can't understand why?!
I already did something similar and it worked...
I know there's other way to do it, but i would like to know why this don't.
Thanks!>
[1327 byte] By [
Jocoa] at [2007-11-27 9:13:13]

I can't see what generates the behaviour you describe. You may want to post some more code, but nicely indented and formatted. One problem with not formatting with the code button above the message field, is when you write an i in square brackets in your code, it is omitted and turns on italic font. The code button will solve this problem.
When addCell() is declared to take an int argument, how can you call it with no argument?
OleVVa at 2007-7-12 22:00:25 >

I see, I didn't know the code button, sorry.
here I post better the code:
CellVector c = new CellVector(size);
for(int i=0;i<size;i++)
c.addCell(i);// truth to be told i is useless, just a sort of unique ID
//Here is the method addCell:
public void addCell(int i){
Cell toadd = new Cell(i);
cells[pos] = toadd;
pos++;
}
// and these are the methods to account for ones and zeros in the string
// they both belong to Cell class
public void addOne(){
bit[1]=bit[1]+1;
}
public void addZero(){
bit[0]=bit[0]+1;
}
// Here the CellVector class
public class CellVector{
public static Cell[] cells;
private static int pos;
public CellVector(int n){
cells = new Cell[n];
pos=0;
}
public void addCell(int i){
Cell toadd = new Cell(i);
cells[pos] = toadd;
pos++;
}
}
//from the main I call
while((line=in.readLine())!=null){
for(int i=0;i<size;i++){
bit=line.charAt(i);
if(bit=='0') myCells.cells.addZero();
else myCells.cells.addOne();
}
}
>
Jocoa at 2007-7-12 22:00:25 >

That's much better (indentation could still be improved a little).
if(bit=='0') myCells.cells.addZero();
else myCells.cells.addOne();
Should that be like below?
if(bit=='0') myCells.cells[i].addZero();
else myCells.cells[i].addOne();
Is myCells the same CellVector that was called c during initialization?
It all looks right to me. Are you saying that if you run it with size 4 and input
0110
0111
then you end up with four cell references where each and every one will tell you there are 3 zeroes and 5 ones?
OleVVa at 2007-7-12 22:00:25 >

> if(bit=='0') myCells.cells.addZero();
> else myCells.cells.addOne();
Should that be
> like below?
> if(bit=='0') myCells.cells[i].addZero();
> else myCells.cells[i].addOne();
ya, sure, I didn't cut and paste that part and just wrote it quickly
> Is myCells the same CellVector that was called c
> during initialization?
idem
> It all looks right to me. Are you saying that if you
> run it with size 4 and input
>
> 0110
> 0111
> then you end up with four cell references where each
> and every one will tell you there are 3 zeroes and 5
> ones?
that exactly what happens, the toy list I used to test it was exactly 2 four-digit strings with 3 zeros and 5 ones and I had four cells each saying "3 zeros" "5 ones".
Jocoa at 2007-7-12 22:00:25 >

I don't understand.
You say i is useless; however, if you store it in each cell, you could use it to determine whether you really have the same object in all the array entries. You may also run it through a debugger, it will be happy to tell you both the value of i and a code for the identity of the object, so you can compare the codes to tell whether you have the same object.
I gather your program is not so big, so you may post all of it (or reduce it to a size where you can post it all).
OleVVa at 2007-7-12 22:00:25 >

(Message regretted, deleted all text)Message was edited by: OleVV
OleVVa at 2007-7-12 22:00:25 >

I will try with debugging.. or I will simply use a matrix. thanks for help!
Jocoa at 2007-7-12 22:00:25 >

> I will try with debugging.. or I will simply use a> matrix. thanks for help!I hope it *was* helpful. Good luck. If you think you need more input, please post here again.
OleVVa at 2007-7-12 22:00:25 >

Sorry if i'm still talking about this problem, but i'm still stuck in it.
To make the thing as clear as possible I post here all the code of an example
that is really clear about what happens.
First of all the Object:
(The methods of interest are the constructor, of course, and addPerm,
even if i will use Print too to see the effects of the problem)
public class Permutations {
private int id = 0;
public static int D;// D should be positive
privateint[] arrayA = new int[D];// by default: initially 0
private boolean[] arrayE = new boolean[D];// by default: initially false
public static int[][]finalset;
private int pos;
public Permutations(int i){
D=i;
arrayA = new int[D];
arrayE = new boolean[D];
pos=0;
int factD=1;
for(int j=0;j<D;j++)
factD=factD*(D-j);
finalset=new int[factD][D];
}
public void printar(int[] arrayA, int size) {
for (int k=0; k><size; k++)
finalset[pos][k]=arrayA[k];
//System.out.print(arrayA[k]+" ");
//System.out.println();
pos++;
}
public void generate() {
int ib = 0;
do {if (arrayE[ib] == false)
{arrayA[id] = ib; arrayE[ib] = true; id++;
if (id == D) {printar(arrayA,D);} else generate();
id--; arrayE[ib] = false;};
ib++;}
while (ib><D);
}
public void convert(int[]alphabet){
int[]sortedperm=finalset[0];
for(int i=0;i<finalset.length;i++)
for(int j=0;j<D;j++){
int symbol=alphabet[finalset[i][j]];
finalset[i][j]=symbol;
}
}
public void genAllPerms(int[]alphabet){
generate();
convert(alphabet);
}
public void genAllPerms(){
generate();
//convert(alphabet);
}
public void print(){
for(int i=0;i<finalset.length;i++){
for(int j=0;j<D;j++)
System.out.print(finalset[i][j]+" ");
System.out.println();
}
}
}
This is an additional object that is a vector of Permutations (you can obtain the same result creating an array of Permutations into the main):
public class PermVector{
public Permutations[] perms;
public PermVector(int p){
perms=new Permutations[p];
}
public void addPerm(int n, int pos){
Permutations toAdd = new Permutations(n);
perms[pos]=toAdd;
}
}
Finally the test main:
public class testPerm {
public static void main(String[] args) {
int k = 3;
PermVector perms = new PermVector(k);
for(int i=0; i><k; i++){
perms.addPerm(5-i, i);
perms.perms[i].genAllPerms();
perms.perms[i].print();
System.out.println();
}
// the output here will shows all possible permutations of 5, 4 and 3 elements
Permutations[] temp = perms.perms;
for(Permutations p : temp) {
System.out.println(p);
}
// here we check that three distinct objects have been created, so it is...
for(int i=0;i<temp.length;i++){
temp[i].print();
System.out.println();
}
//...even if when I check what is inside, it shows that the three object contain
// exactly the same data
}
}
The point is that during the first cycle when I create each "Permutations" obj,
at each iteration all the previously created object are modified.
Can't see why.. please if any of you have any idea let me know.
thanx>
Jocoa at 2007-7-12 22:00:25 >

public static int[][]finalset;I feel (although I didn't really study that code very hard) that the above is your problem.
you mean i should not declare it static?
Jocoa at 2007-7-12 22:00:25 >

Well, all permutation classes will be using the same (and I do mean the same) finalset variable, and since that is what you are printing out in that print method, then, to me, it is quite obvious why they all print the same value. That is what "static" means.
Thank you so much!! you're right i tried and now it work greatly.thanks again
Jocoa at 2007-7-12 22:00:25 >
