Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Returning method which returns arraylist?

Tags:

java

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?

like image 592
Nikhil Avatar asked Jul 27 '12 05:07

Nikhil


People also ask

What is the return type of ArrayList in Java?

It returns true if the list contains the specified element. It is used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.

How do you return an ArrayList from a method?

Return ArrayList in Java From a Static Method You can call a static method using the class name. Returning an ArrayList from a static method is simple. Create a static method with the return type as ArrayList . Create an ArrayList and instantiate it.

What does ArrayList get return?

The Java ArrayList get() method returns the element present in specified position.

What is the return type of list in Java?

The list() method of java. util. Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.


2 Answers

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.
 }


}
like image 139
Kumar Vivek Mitra Avatar answered Sep 23 '22 07:09

Kumar Vivek Mitra


Assuming you have something like so:

public class MyFirstClass {
   ...
   public ArrayList<Integer> myNumbers()    {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(5);
    numbers.add(11);
    numbers.add(3);
    return(numbers);
   }
   ...
}

You can call that method like so:

public class MySecondClass {
    ...
    MyFirstClass m1 = new MyFirstClass();
    List<Integer> myList = m1.myNumbers();
    ...
}

Since the method you are trying to call is not static, you will have to create an instance of the class which provides this method. Once you create the instance, you will then have access to the method.

Note, that in the code example above, I used this line: List<Integer> myList = m1.myNumbers();. This can be changed by the following: ArrayList<Integer> myList = m1.myNumbers();. However, it is usually recommended to program to an interface, and not to a concrete implementation, so my suggestion for the method you are using would be to do something like so:

public List<Integer> myNumbers()    {
    List<Integer> numbers = new ArrayList<Integer>();
    numbers.add(5);
    numbers.add(11);
    numbers.add(3);
    return(numbers);
   }

This will allow you to assign the contents of that list to whatever implements the List interface.

like image 30
npinti Avatar answered Sep 23 '22 07:09

npinti