Java - How to access an ArrayList of another class?

后端 未结 5 514
感动是毒
感动是毒 2020-12-23 18:11

Hello I\'m a beginner in Java and this is my question: I have this first class with the following variables:

import java.util.ArrayList;

public class number         


        
5条回答
  •  伪装坚强ぢ
    2020-12-23 18:36

    import java.util.ArrayList;
    public class numbers {
       private int number1 = 50;
       private int number2 = 100;
       private List list;
    
       public numbers() {
           list = new ArrayList();
           list.add(number1);
           list.add(number2);
       }
    
       public List getList() {
           return list;
       }
    }
    

    And the test class:

    import java.util.ArrayList;
    public class test {
       private numbers number;
    
       //example
       public test() {
         number = new numbers();
         List list = number.getList();
         //hurray !
       }
    }
    

提交回复
热议问题