Objects lost in the array

I am creating an RPG style program but I am having a hard time getting my treasure object array to work. I want to keep all the treasures I find in an array to be printed later. Here is the code for the treasure class:

private static int x = 0;
Treasure treasureArray[] = new Treasure[20];

public void collectedTreasures(Treasure t){
treasureArray[x] = t;
x++;
}

      

And in the main program:

GoldTreasure t = new Coin();
hero1.setPoints(t.getCoin());
t.collectedTreasures(t);

      

The creation of the treasure object is inside the switch in an endless loop. When I print the array using the method

public void printTreasures(){
        for (int y=0 ; y<x ; y++){
            System.out.print(treasureArray[y] + ", ");

      

I only get "zero" for as many treasures as there should be in the array. If I print the array after t.collectedTreasures (t), I see that only the last treasure exists, and the indices before that object are zero. What I did wrong?

Yes, I'm a beginner. Please be kind.

+3


source to share


3 answers


This code is rather suspicious:

GoldTreasure t = new Coin();
hero1.setPoints(t.getCoin());
t.collectedTreasures(t);

      

This means that you:



  • creation of a new treasure t

    ;
  • call collectedTreasures

    in this very instance.

You must assign the treasure array to the hero, not the treasure itself.

Also note that x

it shouldn't be a static variable because it will be shared among all instances; clearly not your intention as the treasure array is an instance.

+6


source


The problem is that you collect the treasure yourself, because you call the collect function - which belongs to the treasure - you collect yourself.

Later when you call printTreasures on which object is it being run? Do you create a new instance of the treasure and ask him to print out what he collected? If so, the results match the code and there is no problem with it, but the logic is faulty.



What you have to do: The hero is the one who collects the treasures, so move the definition of the treasure array, counter and two functions - collectTreasures and printTreasures - to the hero class. Also, make X non-static, as its value will be shared between heroes. Perhaps even more ellegant, create an additional treasure handling class and compose a hero for you using different classes.

And I may suggest renaming the collectTreasures (Treasure t) function to collect data (Treasure t).

0


source


We'll need to see the complete code to give you a detailed answer, but I suspect you are doing a lot of subclasses Treasure

and calling collectedTreasure

on each one. This will increment your global counter each time x

, whereas each individual treasureArray

has only one entry.

You can move the method collectedTreasure

to the class corresponding to your object hero1

, at the same time get rid of the static (global) variable x

and replace the array of Treasure

objects with List

(for example ArrayList

), they all keep track of their own sizes, so you don't need to. Plus, your code won't crash when you get over 20 treasures!

0


source







All Articles