I could not locate the package in which Java defines raw arrays like String[] strs ( not ArrayList).
What methods and properties are defined in such Java array and how do I return an iterator for such array supposed I am asked to return an iterator for two integers begin and end?
Here's a good summary:
https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
10.7 Array Members
The members of an array type are all of the following:
The public final field length, which contains the number of components of the array (length may be positive or zero)
The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions
All the members inherited from class Object; the only method of Object that is not inherited is its clone method
As far as "iterators"; the "beginning" and "end" are simply "0" and ".length - 1". You can always implement your own class that wraps an array and implements Iterator.
The only properties available (specific to Arrays) are really .length
, and the index accessor, e.g. [0]
.
Arrays can be used in the new for loop syntax provided with Java 1.5:
for(String s : new String[]{"a", "b", "c"}){
// Something with s.
}
You can also access an Array as a List, using http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList%28T...%29 .
Also see the rest of the Arrays class for many other operations that exist to operate specifically on arrays. (Here, we have a class that operates on arrays, rather than the array containing all of the useful properties and methods.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With