What methods and properties are available in the Java array, for example String [] strs?

I could not find a package in which Java defines raw arrays such as String [] strs (not ArrayList).

What methods and properties are defined in such a Java array and how to return an iterator for such an array? It is assumed that I am invited to return an iterator for two integers starting and ending?

+4
source share
2 answers

Here is a good summary:

https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html

10.7 Array Members

Array type elements are as follows:

  • The total final length of the field, which contains the number of components in the array (the length can be positive or zero)

  • An open method clone that overrides a method with the same name in a class object and does not throw exceptions

  • All members inherited from the Object class; the only Object method that is not inherited is its clone method

As for the "iterators"; "start" and "end" are simply "0" and ".length - 1". You can always implement your own class, which wraps an array and implements Iterator .

+4
source

The only properties available (array-specific) are indeed .length , and an index accessory, for example. [0] .

Arrays can be used in the new for loop syntax provided by Java 1.5:

 for(String s : new String[]{"a", "b", "c"}){ // Something with s. } 

You can also access the 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 work directly with arrays. (Here we have a class that works with arrays, and not with an array containing all the useful properties and methods.)

+2
source

Source: https://habr.com/ru/post/1383517/


All Articles