Nikhil
Nikhil

Reputation: 2883

Java Returning method which returns arraylist?

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?

Upvotes: 19

Views: 377779

Answers (9)

Aritn faghanzadeh
Aritn faghanzadeh

Reputation: 1

you can also create method like prime_f and then call ArrayList like:

   public static ArrayList<Integer> prime_f(int number) {
   ArrayList<Integer> counter = new ArrayList<Integer>();
   for (int i = 2;i < number;i++) {
       while(number%i == 0) {
           counter.add(i);
           number = number/i;
       }
   }
   if (number > 2) {
       counter.add(number);
   }
   return counter;
}

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

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


}

Upvotes: 24

kyle
kyle

Reputation: 3

Your method can be called and the arraylist can be stored like this

YourClassName class = new YourClassName();
Arraylist<Integer> numbers = class.numbers(); 

This also allows the arraylist to be manipulated further in this class

Upvotes: 0

Dominic Bou-Samra
Dominic Bou-Samra

Reputation: 15416

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();

Upvotes: 8

user1464472
user1464472

Reputation:

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

Upvotes: 1

npinti
npinti

Reputation: 52185

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.

Upvotes: 9

Nirmal- thInk beYond
Nirmal- thInk beYond

Reputation: 12054

You can use on another class

public ArrayList<Integer> myNumbers = new Foo().myNumbers();

or

Foo myClass = new Foo();
 public ArrayList<Integer> myNumbers = myclass.myNumbers();

Upvotes: 0

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

MyClass obj = new MyClass();
ArrayList<Integer> numbers = obj.myNumbers();

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240900

If Foo is the class enclose this method

class Foo{
  public ArrayList<Integer> myNumbers()    {
     //code code code
  }
}

then

new Foo().myNumbers();

Upvotes: 1

Related Questions