Problem with array list in Java

Started by
13 comments, last by Zahlman 14 years, 9 months ago
Quote:Original post by rip-off
Why are you creating a new Federation instance in controlActivities? If you look at your code, you have 2 Federation instances, one created inside main() and another in controlActivities().


Because I don't want to control the program from inside the main method, so I created the first Federation just to it call controlActivities, and from there I do the Federation's management

Advertisement
Well, your problem stems from having two Federation instances. You are adding elements to the vector in one and printing them from another.

If you want to move code away from main() you need to create additional classes or functions that do what you want. But that is window dressing, the structure is pretty similar no matter where the code is.

Try this:
public void controlActivities() {	    		    	this.addsTournaments (this.createsTournaments("rolandGarros",200,140,90,50));	    	this.addsTournaments (this.createsTournaments("umag",30,20,15,10));	    	System.out.println(tournamentManager.get(0).retrievesChampion());		}

Note that the use of the "this" reference is unnecessary, but is used mainly for illustration.
Quote:Original post by rip-off
Well, your problem stems from having two Federation instances. You are adding elements to the vector in one and printing them from another.

If you want to move code away from main() you need to create additional classes or functions that do what you want. But that is window dressing, the structure is pretty similar no matter where the code is.

Try this:
*** Source Snippet Removed ***
Note that the use of the "this" reference is unnecessary, but is used mainly for illustration.


It worked. I am somewhat confused about what really is going on. When "federationn" inside main calls controlActivities, all the objects created inside this method are going to belong to it?
So the "federation" created inside controlActivities is part of "federationn"?

I thought objects, created when an object call's a method that creates objects, didn`t belong to anybody, they just "existed".

Or not?
tournamentManager in my example is trying to get objects from where?
Are there two copies of tournamentManager, one belonging to "federation" and other to "federationn"?

I think I am understanding right now, "federation" doesn't call System.out.println, and since it is called from inside controlActivities, which, by it's turn, is called by "federationn", in the end is "federationn" that is calling system.out. Due to it, tournamentManager tries to get it's tournament objects, but there is nothing there, they belong to the tournamentManager of "federation", because it was "federation" that created those tournaments and added them to it's "own" tournamentManager list.

At least from a logical point of view this seems to make sense.
I think you're having trouble understanding the distinction between variables and values.

When you write 'new Foo()', you create a value. When you write 'x = new Foo();', you create the value, and then assign it to the variable. This assignment gives the value a name, locally.

Multiple values can have the same name. They are still distinct values.
The same value can have multiple names. This happens in two ways: by passing the value around (e.g. you pass it to a function, and the function has its own name for the value - specifically, the parameter name), and by aliasing (e.g. 'x = new Foo(); y = x;' - here, 'x' and 'y' refer to the same Foo, and state changes in 'x' are reflected by 'y', because they're the same thing - for now).

In Java, values exist as long as they have a name (i.e., as long as any variable or parameter refers to it). A variable ceases to exist at the end of its scope; at that point, it no longer names its value, but the value may still be "kept alive" by some other variable.

public static void inner(Foo f) { // at this point, 'f' refers to the 'g' of outer().  Foo g = f; // This creates a 'g' separate from the one in outer().  // There are now three references to the Foo. Any of these "sees"  // a 'mutated' Foo, because they refer to the instance that had .mutate()  // called on it in outer().} // At this point, 'f' and 'g' cease to exist, but the passed-in Foo remains.public static void outer() {  Foo g = new Foo();  g.mutate(); // suppose this makes some change to the Foo.  inner(g);} // the 'g' of outer() ceases to exist, allowing the garbage collector to// reclaim the memory.public static void main(String[] args) {  Foo g = new Foo(); // this is a separate Foo from the one that will be  // created within outer().  outer();  // The 'g' of main() still exists, and is not 'mutated', because it's a  // different Foo from the one that was created in outer().}


Now to your code.

main() creates main()'s federationn, and calls controlActivities() on it. controlActivities() creates controlActivities()'s federation, and calls createsTournaments() and addsTournaments() on it. The federationn in main() is unaffected by this. At the end of controlActivities(), there are no references to controlActivities()'s federation, so it may now be cleaned up.

By the way, normal function names would be addTournaments() and createTournaments() - note the removed 's's. The functions are named as commands (instructions), not descriptions. They're what you're telling the object to do. "Federation, add tournaments (to yourself)".

This topic is closed to new replies.

Advertisement