返回arraylist的Java返回方法? [英] Java Returning method which returns arraylist?

查看:2201
本文介绍了返回arraylist的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?

推荐答案

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。使用撰写

3。最好有一个 Generic ArrayList ,如 ArrayList< Integer> 等...

3. It would be better to have a Generic ArrayList like ArrayList<Integer> etc...

例如:

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


}

这篇关于返回arraylist的Java返回方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆