简体   繁体   中英

How do I pull value from method designated with “this.”

I have information like this:

xxx      0      1      2      ...
Name     Fred0  Fred1  Fred2
Stamina  2      6      7
Intel    5      4      1
Heart    4      8      2
Speed    5      3      6

So, I was informed previously that creating a 2D ArrayList to store something like this is "archaic" and was provided with a different way to set my code up. The reason I was using ArrayList<> is because I want to be able to generate new racers as needed, rather than designating an array to a size. If I could just use a simple array this would have been done a week ago. Now I've been screwing with it for a week and I still don't get how it works.

public class test {
    public String name;
    private int stamina;
    private int heart;
    private int intel;
    private int speed;
    public ArrayList<String> racers = new ArrayList<String>();

    private void racerInfo(String name) {
        this.name = name;
        this.stamina = (int) Math.floor(Math.random()*10);
        this.heart = (int) Math.floor(Math.random()*10);
        this.intel = (int) Math.floor(Math.random()*10);
        this.speed = (int) Math.floor(Math.random()*10);
    }

    public void generate() {
        for ( int i=0; i<=10; i++) {
            String name = "Fred" + i;
            System.out.println(name);
            racerInfo(name);
            racers.add(name);
        }
    }

    public int getStamina() {
        return this.stamina;
    }
    public int getHeart() {
        return this.heart;
    }
    public int getIntel() {
        return this.intel;
    }
    public int getSpeed() {
        return this.speed;
    }
}

public class main {
    public static test test = new test();

    public static void main(String[] args) {
        test.generate();
        //Put stuff here to pull stamina of Fred2 for example.
    }
}

Now, in the main class. How would I do something that should be relatively simple like pulling the Stamina value for Fred2. I've been following the exact directions I've been given by others here to write most of this code. But at this time, I'm getting to the point of just re-writing it all so that each stat (name, stamina, intel, speed, etc.) is just logged as a separate ArrayList<> . But I can't figure out how to make a 2D ArrayList containing the original ArrayLists ie.

ArrayList<String> name = new ArrayList<String>();
ArrayList<Integer> stamina = new ArrayList<Integer>();
ArrayList<ArrayList<Object>> arrayOfArray = new ArrayList<ArrayList<Object>>();

Yes, I know the arrayOfArray is probably done wrong, but, again, I just get told it's Archaic and nobody'll tell me how I can do it right so I can just go. arrayOfArray.get(2,1) and pull information that I want/need.

Sorry for the information overload here. but I'm trying to just find the best possible solution for what I want to do. If you can tell me how to correctly pull off either way I will be eternally grateful you you and all of your descendants.

First of you should refactor your class test to class Racer, which is a meaningful name and follows the convention to start classnames with an uppercase letter. Furthermore you should add Stamina, Intel, Heart and Speed to the constructor:

public Racer(String name, int stamina, int intel, int heart, int speed) {
    this.name = name;
    this.stamina = stamina;
    this.intel = intel;
    this.heart = heart;
    this.speed = speed;
}

Now you can create your racer as following:

Racer fred2 = new Racer("Fred2", 7, 1, 2, 6);

You can store your values in a HashMap. HashMap is a collection consisting of key-value pairs. For the key you can use a string (the name of the racer) and as value you take an instance of your class Racer:

HashMap<String, Racer>() racerMap = new HashMap<>();
racerMap.put("Fred2", fred2);

This you can do in a for-loop for all of your racers. Now you can get the racer objects from your HashMap by calling the getMethod and putting the name as parameter in it. This will return an object of class Racer and you can call the getter methods on this object:

racerMap.get("Fred2").getSpeed();

or

racerMap.get("Fred2").getIntel();

Edit: I just saw your generate method. This method should return the HashMap of racers. In your main method you create a new HashMap:

HashMap<String, Racer> racerMap = generate();

Now you can use the map as described above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM