I want to pick first N elements from a list in java
String[] ligature = new String{there,is not,a,single,person,who,knows,the,answer};
Now I want to pick first 5 elements from this.something like
Stiring[] selectedLigature = ligature[0:4];
I want to do it without using for loop.
List. You could do:Arrays.asList(ligature).subList( 0,5 ).toArray(); But that is cumbersome..toArray(new String[5])would be needed if one wants to get a typed array backtoArray(new String[0])as the argument is only used to determine the type.Listthen it is used. At least in the case ofArrayList.nullif it is bigger. Thanks!