Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pick first N elements from a static list java [duplicate]

Tags:

java

arrays

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.

like image 590
user_3pij Avatar asked Oct 12 '25 20:10

user_3pij


2 Answers

Do not stream this simple case, there is subList for this:

// for a List
yourList.subList(0, 5)...

// for an array
Arrays.copyOfRange
like image 188
Eugene Avatar answered Oct 14 '25 09:10

Eugene


Arrays class has a method for this:

Arrays.copyOfRange(ligature, 0, 5);
like image 27
Jack Flamp Avatar answered Oct 14 '25 08:10

Jack Flamp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!