• Register

ArrayList is resizeable that makes it better than a simple array. Also, you can print the whole ArrayList using a single line of code. This flexibility and versatility make it easier to use and manipulate.  

ArrayList Example

​import java.util.ArrayList;

public class Arraylist {

	public static void main(String[] args) {
		//Creating arraylist    
		  ArrayList<String> fruits=new ArrayList<String>();
		  fruits.add("Mango");//Adding object in arraylist    
	     //Printing the arraylist object   


Returning an ArrayList using method: 

Returning an ArrayList is quite convenient. All you need to do is use the 'return' keyword. The method that returns nothing uses the keyword 'void' in the method declaration else it needs a return type for this purpose. For instance, a method that returns integer values uses 'int' as a return type. Likewise, if you want to return an ArrayList, all you have got to do is to use 'ArrayList' as a return type while declaring a method.  


public ArrayList thisReturnsAnArrList(ArrayList list) {

    return list; 


In order to return ArrayList in the above example, declare and define a return method i.e.

public ArrayList return_method(ArrayList fruits) {
    	//Return Arraylist
	      return fruits;

Two important things you need to notice here are

  1. Return type: Function used 'ArrayList' as a return type
  2. Parameter: Before we can get an ArrayList from a method, we need to provide this method a parameter of type 'ArrayList'

Now, you can call this method in its class as well as you can access it in another class(Public methods are accessible in other classes).





100 points