A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/abstractlist-adde-ele-method-in-java-with-examples/ below:

AbstractList add(E ele) method in Java with Examples

AbstractList add(E ele) method in Java with Examples

Last Updated : 17 Dec, 2018

The add(E ele) method of AbstractList class in Java is used to insert the specified element to the end of the current list.

Syntax

:

public boolean add(E ele) Where E is the type of element maintained by this AbstractList collection. Parameter

: This method accepts a single parameter

ele

which represents the element to be inserted at the end of this list.

Return Value

: The function returns a boolean value True if the element is successfully inserted in the List otherwise it returns False.

Exceptions

:

Below programs illustrate the AbstractList.add(E ele) method:

Program 1

:

Java
// Java code to illustrate add(Object o)
import java.io.*;
import java.util.*;

public class AbstractListDemo {
    public static void main(String[] args)
    {

        // create an empty list with an initial capacity
        AbstractList<Integer> list = new ArrayList<Integer>(5);

        // use add() method to add elements in the list
        list.add(15);
        list.add(20);
        list.add(25);

        // prints all the elements available in list
        for (Integer number : list) {
            System.out.println("Number = " + number);
        }
    }
}
Output:
Number = 15
Number = 20
Number = 25
Program 2

:

Java
// Java code to illustrate add(Object o)
import java.io.*;
import java.util.*;

public class ArrayListDemo {
    public static void main(String[] args)
    {

        // create an empty list with an initial capacity
        AbstractList<String> list = new ArrayList<String>(5);

        // use add() method to add elements in the list
        list.add("Geeks");
        list.add("For");
        list.add("Geeks");

        // prints all the elements available in list
        for (String str : list) {
            System.out.print(str + " ");
        }
    }
}
Reference

:

https://docs.oracle.com/javase/7/docs/api/java/util/AbstractList.html#add(E)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4