I would like to return an arraylist from a non-class method in java, i have searched around but found nothing so far.
{
int d=e.length();
ArrayList list2 = new ArrayList();
for (int i=0;i<d;i++)
{
list2.add('_');
}
return list2;
}
Thanks alot!
All you need to do is put the return type in the signature, and then return the list
Note that because
ArrayListextendsListyou can make the return type either one of those.Listis more generic, you can return any type of list. This is useful because if you want to change the internal list that you use (say change fromArrayListtoLinkedList) you won’t have to change your API everywhere you use the method.However, if you use a list implementation that has its own methods that are not defined by the
Listinterface, you won’t be able to access those methods when you get the list back from your method, unless you either cast (ewe) or specify the exact return type.