Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Arrays - What are they

Tags:

java

arrays

From looking at the Java Collections API i see that arrays are not regarded as collections. If not what are arrays regarded as?

like image 422
user253506 Avatar asked May 25 '26 11:05

user253506


1 Answers

Arrays are "special" in Java - they don't implement any interfaces, which means they can't implement the collection interfaces. They're collections in "natural language" terms, and you can use the enhanced for loop over them - but if you want to use an array within the collection API, you'll need something like Arrays.asList which wraps an array with the List<T> interface. (The result is only a view on the array - changes to the array are visible through the list, and vice versa.)

(This is in contrast to .NET, where T[] implements IList<T> etc.)

like image 107
Jon Skeet Avatar answered May 28 '26 00:05

Jon Skeet



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!