Menu
Topics Index
...
`

Arrays asList Test - Java Example Program


Arrays asList Test
import java.util.Arrays;

class ArraysAsListTest
{
    public static void main(String arg[])
    {
        String sample[] = {"MeritCampus", "Siva", "Mahesh", "Kiran"};
        List<String> sampleList = Arrays.asList(sample); // LINE A
        System.out.println(sampleList); // LINE B    
    }
}
OUTPUT

[MeritCampus, Siva, Mahesh, Kiran]

DESCRIPTION

At LINE A, the string array is converted to the list sampleList, which holds all the elements present in array sample. LINE B prints the elements present in the list, same as the elements in the array.

THINGS TO TRY
  • Add elements to the list sampleList after LINE B and print the elements in the list.
  • Remove elements from the list sampleListand print the elements in the list.

© meritcampus 2019

All Rights Reserved.

Open In App