1

I am very new to java programming and I am trying to create a RPG game. What I am looking to do is create a unit (playable character) that can be modified by item parameters (item stats). It seams to me it would be simple to use an array as an object parameter and then reference its contents through methods. An example of this might be having an int array that has a parameter that is used to multiply the character’s base damage or increase armor amount. My question is this, is it possible to use an array as an object parameter in the way I described? Here is an example of what the object constructor might look like:

public class Ranger {
    public Ranger(String name, int baseDamage, int[] item) {
        /* ... */
    }
}

Also, could anyone advise if there is a better system in general to accomplish what I’m trying to do?

1 Answer 1

3

Yes, there's a better way. Use a collection.

public class Ranger {
    public Ranger( String name, int baseDamage, Collection<Item> inventory) {
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

You have totally delivered! Thank you for sharing this with me. It’s been tough to find this information because whenever I would search for something like “array as object parameter” or what have you I would be directed to results focused on passing arrays to methods which was not very helpful. Thanks again!
@DarthBusiness a constructor is a special kind of a method, so anything you can pass to a method you can also pass to a constructor. But instead of searching, you should be going through the Java tutorial on Oracle's site which would cover these kinds of things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.