Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What methods and properties are available in Java array like String[] strs?

Tags:

java

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?

like image 667
ace Avatar asked Nov 28 '11 20:11

ace


2 Answers

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.

like image 70
paulsm4 Avatar answered Oct 29 '22 18:10

paulsm4


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.)

like image 20
ziesemer Avatar answered Oct 29 '22 17:10

ziesemer