I have one class with a method like this:
public ArrayList myNumbers() {
ArrayList numbers = new ArrayList();
numbers.add(5);
numbers.add(11);
numbers.add(3);
return(numbers);
}
how can i call this method inside another class?
解决方案
1. If that class from which you want to call this method, is in the same package, then create an instance of this class and call the method.
2. Use Composition
3. It would be better to have a Generic ArrayList like ArrayList etc...
eg:
public class Test{
public ArrayList myNumbers() {
ArrayList numbers = new ArrayList();
numbers.add(5);
numbers.add(11);
numbers.add(3);
return(numbers);
}
}
public class T{
public static void main(String[] args){
Test t = new Test();
ArrayList arr = t.myNumbers(); // You can catch the returned integer arraylist into an arraylist.
}
}