A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

How to extract some of the elements from given list in Java?
Q:

How to extract some of the elements from given list in Java?

0

Given a list and we have to extract elements from to index to from index using java program.

Example:

    Input:
    Original list: [One, Two, Three, Four, Five]
    Now, we have to extract elements from 0 to 3 index (first 3 elements 

    Output:
    List of first three elements: [One, Two, Three]

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to extract elements from a list in java

import java.util.ArrayList;
import java.util.List;

public class ExArrayExtract {
  public static void main(String[] args) {
    // Create a list and add some elements to the list.
    List < String > list_Strings = new ArrayList < String > ();

    list_Strings.add("One");
    list_Strings.add("Two");
    list_Strings.add("Three");
    list_Strings.add("Four");
    list_Strings.add("Five");

    // print the original string.
    System.out.println("Original list: " + list_Strings);

    // string extracted from given index.
    List < String > sub_List = list_Strings.subList(0, 3);

    // print extracted string.
    System.out.println("List of first three elements: " + sub_List);
  }
}

Output

Original list: [One, Two, Three, Four, Five]
List of first three elements: [One, Two, Three]

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now