Issue
I have one class with a method like this:
public ArrayList<Integer> myNumbers() {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(11);
numbers.add(3);
return(numbers);
}
how can i call this method inside another class?
Solution
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<Integer>
etc...
eg:
public class Test{
public ArrayList<Integer> myNumbers() {
ArrayList<Integer> numbers = new ArrayList<Integer>();
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<Integer> arr = t.myNumbers(); // You can catch the returned integer arraylist into an arraylist.
}
}
Answered By - Kumar Vivek Mitra
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.