Java Returning method which returns arraylist?

298,529

Solution 1

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


}

Solution 2

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.

Solution 3

You need to instantiate the class it is contained within, or make the method static.

So if it is contained within class Foo:

Foo x = new Foo();
List<Integer> stuff = x.myNumbers();

or alternatively shorthand:

List<Integer> stuff = new Foo().myNumbers();

or if you make it static like so:

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

you can call it like so:

List<Integer> stuff = Foo.myNumbers();

Solution 4

List<Integer> numbers = new TheClassName().myNumbers();

Solution 5

MyClass obj = new MyClass();
ArrayList<Integer> numbers = obj.myNumbers();
Share:
298,529

Related videos on Youtube

Nikhil
Author by

Nikhil

Updated on July 28, 2022

Comments

  • Nikhil
    Nikhil almost 2 years

    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?

  • npinti
    npinti almost 12 years
    I think you mean arr = t.myNumbers();?

Related