Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

java - Adding enum type to a list

If I need to add an enum attribute to a list, how do I declare the list? Let us say the enum class is:

public enum Country{ USA, CANADA; }

I want to do:

List<String> l = new ArrayList<>();
l.add(Country.USA);

What needs to be used instead of List<String>?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Should be:

List<Country> l = new ArrayList<Country>();
l.add(Country.USA); // That one's for you Code Monkey :)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...