Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cruzrojas

macrumors member
Original poster
Mar 26, 2007
67
0
USA
Does anyone here have an Idea what I might be doing wrong here.

This is quite a long program so I'm gonna try to simplify it here.

I have a ClassA, with a private double var.
I have a ClassB, ClassB stores one element of ClassA.
I have a ClassC extends Thread, ClassC stores one or more elements of ClassB.

I have another class ClassSort, witch is just an array of 50 elements of ClassA and it sort it as they are being entered according to the value of var.

Now this is the way Im trying to run it.

Code:
if(classCList.size() == 15){
   while(classCList.size() != 0){
      if(!classCList.get(0).isRunning()){
         for(int j = 0; j < classCList.get(0).getNumberOfClassBElements(); j++){
            tempClassA = classCList.get(0).getClassB(j).getClassA();
	    if(tempClassA.getVar() > 0.01){
	       try{Thread.sleep(1000);}catch(InterruptedException e){return;}
	       System.out.println("Adding : " + tempClassA.getVar());
	       classSortType.add(tempClassA);
	    }
         }
         classCList.remove(0);
      }
   }
}

When I try and run my program, I start with classSortType fill with value bigger than 0. After running the program I print out the vars stored in the array, and most of them have been set to zero for some strange reason... I can't find where this problem is comming from, do you have any ideas where is my problem, or what can I do to track down the problem.

If you need more details about the classes ask me.

Thank you all in advance.
 

cruzrojas

macrumors member
Original poster
Mar 26, 2007
67
0
USA
Also, when I follow the code line by line in the Xcode debugger, using the step into tool, at some point in my code it sends me to another part of my ClassSort File, but it is a commented line. It is a line that used to be commented using //
inside of a method I latter on commented in the whole by /* */
 

cruzrojas

macrumors member
Original poster
Mar 26, 2007
67
0
USA
For shame on me, After I post this I found my mistake. The reason is not in the code included, the reason is that before that while I'm making the instances of ClassC I have to give it instances of ClassA, I grab those classes out of the ClassSort by

ClassA newClassA;
newClassA = ClassSort.get(index);
newClassA.reset();

this modifies the ClassA stored in the array, it resets it. We might say that the second line is giving me a pointer to the class stored in the array instead of making a new copy of it. Does any one have an idea why is this. My solution was to make a method copy whitin ClassA.

ClassA newClassA = new ClassA();
newClassA.copy(ClassSort.get(index));
newClassA.reset();

Best Regards
cruzrojas
 

Allanist

macrumors newbie
Mar 18, 2007
25
0
For shame on me, After I post this I found my mistake. The reason is not in the code included, the reason is that before that while I'm making the instances of ClassC I have to give it instances of ClassA, I grab those classes out of the ClassSort by

ClassA newClassA;
newClassA = ClassSort.get(index);
newClassA.reset();

this modifies the ClassA stored in the array, it resets it. We might say that the second line is giving me a pointer to the class stored in the array instead of making a new copy of it. Does any one have an idea why is this. My solution was to make a method copy whitin ClassA.

ClassA newClassA = new ClassA();
newClassA.copy(ClassSort.get(index));
newClassA.reset();

Best Regards
cruzrojas
Because Java doesn't have automagical copystuff. What does ClassSort.get(index) do, returns an object from the array? If it does, it really returns the object from the array, not copying it. More precisely it returns a reference to the same object in the array. You have to make a copy explicitly if you want to have a copy...
 

cruzrojas

macrumors member
Original poster
Mar 26, 2007
67
0
USA
Yes it does return an element from the array. Thanks for your comment, and yes I will remember from now on, that doing that will make a reference to the element and not an actual copy. It would be nice to have a compilation error if you try to do things like this (I think C++ would but I'm not sure). But people at sun might think that you know what are you doing.:(
 

aaronbrethorst

macrumors member
Feb 1, 2007
30
0
Seattle, WA
Yes it does return an element from the array. Thanks for your comment, and yes I will remember from now on, that doing that will make a reference to the element and not an actual copy. It would be nice to have a compilation error if you try to do things like this (I think C++ would but I'm not sure). But people at sun might think that you know what are you doing.:(

Joel Spolsky wrote about this a while ago. He's well worth reading if you don't already. I've been bitten in similar ways about a bajillion times :)

Cheers,
Aaron
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.