Retrieve Specific Element From ArrayList | Java Programs

Output:

*** Retrieve Specific Element From ArrayList ***

[F16sw18, F16sw40, F16sw44, F16sw50, F16sw84, F16sw12]
Second element: F16sw40
Sixth element:  F16sw12

Click Here For Java Online Compiler

Solution:

/**
 *
 * @author Alee Ahmed Kolachi
 */
import java.util.ArrayList;

public class RetrieveElementArrayList {

    public static void main(String[] args) {
        System.out.println("*** Retrieve Specific Element From ArrayList ***" + "\n");
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("F16sw18");
        arrayList.add("F16sw40");
        arrayList.add("F16sw44");
        arrayList.add("F16sw50");
        arrayList.add("F16sw84");
        arrayList.add("F16sw12");

        System.out.println(arrayList);

        // Retrive the first and third element
        String element = arrayList.get(1);
        System.out.println("Second element: " + element);
        element = arrayList.get(5);
        System.out.println("Sixth element:  " + element);
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon