Help with Java

Discussion in 'Mac Programming' started by cruzrojas, May 5, 2007.

  1. cruzrojas macrumors member

    cruzrojas

    Joined:
    Mar 26, 2007
    Location:
    USA
    #1
    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.
     
  2. cruzrojas thread starter macrumors member

    cruzrojas

    Joined:
    Mar 26, 2007
    Location:
    USA
    #2
    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 /* */
     
  3. cruzrojas thread starter macrumors member

    cruzrojas

    Joined:
    Mar 26, 2007
    Location:
    USA
    #3
    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
     
  4. Allanist macrumors newbie

    Joined:
    Mar 18, 2007
    #4
    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...
     
  5. cruzrojas thread starter macrumors member

    cruzrojas

    Joined:
    Mar 26, 2007
    Location:
    USA
    #5
    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.:(
     
  6. GavinT macrumors member

    #6
    That wouldn't make any sense. In other scenarios, you may well be wanting to assign another variable with a reference to an object, rather than making a copy of it.
     
  7. aaronbrethorst macrumors member

    Joined:
    Feb 1, 2007
    Location:
    Seattle, WA
    #7
    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
     

Share This Page