Given a value N, the task is to create a List having this value N in a single line in Java.
Examples:
Input: N = 5
Output: [5]
Input: N = GeeksForGeeks
Output: [GeeksForGeeks]
Approach:
- Get the value N
- Create an array with this value N
- Create a List with this array as an argument in the constructor
Below is the implementation of the above approach:
import java.io.*;
import java.util.*;
class GFG {
public static <T> List<T> createList(T N)
{
int size = 1 ;
T arr[] = (T[]) new Object[ 1 ];
arr[ 0 ] = N;
List<T> list = Arrays.asList(arr);
return list;
}
public static void main(String[] args)
{
int N = 1024 ;
System.out.println( "List with element "
+ N + ": "
+ createList(N));
String str = "GeeksForGeeks" ;
System.out.println( "List with element "
+ str + ": "
+ createList(str));
}
}
|
Output:
List with element 1024: [1024]
List with element GeeksForGeeks: [GeeksForGeeks]