1

Suppose that I have created a list of names, eg:

Martin 
Paul 
Diego 
Damian 
etc. 

And I need to initialize respectively, for example (psuedocode):

Martin int = 0; 

And you create them an Output same for all, which is (psuedocode):

System.out.println (Martin + "Martin foundn"; 

How to do this automatically?

2 Answers 2

3

You're looking for a HashMap, which maps keys to their values.

HashMap<String, Integer> map = new HashMap<>();
map.put("Martin", 0);
// etc
System.out.println(map.get("Martin"));

You can see the official documentation for what you can do this with, but this is the class you will need specifically.

Sign up to request clarification or add additional context in comments.

Comments

1

You could define them as an Enum with (hopefully immutable) behaviour:

enum Person {
    MARTIN {
        public String pName() {
            return "something here";
        }
        public int pNumber() {
            return 0;
        }
    },
    PAUL {
        public String pName() {
            return "something else";
        }
        public int pNumber() {
            return 1;
        }
    };

    public abstract String pName();
    public abstract int pNumber();
}

2 Comments

@Unihedron: now in uppercase,
This works and could be what OP wants if they're looking for immutable behaviour.

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.