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
433 views
in Technique[技术] by (71.8m points)

arraylist - Java sharing same values in a class

I have a class called Hand and another class to test it. Hand uses global variables and change their values with some methods if in class test I create two variables of the Hand class changes in one of the variables will affect the other. How can I make them separate ?

Class Hand:

private static List<Card> hand = new ArrayList<Card>();

Class Test:

        Hand hand1 = new Hand();
        Hand hand2 = new Hand();

If I add values to hand1 arraylist it changes also the values of hand2 arraylist. Can I separate them?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Turn the private static List<Card> into a private List<Card>.


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

...