Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There really isn't a good answer. An iterator in Java specifically applies to a collection of discrete items (objects). You would think that a <code>String</code>, which implements <code>CharSequence</code>, should be a "collection" of discrete characters. Instead, it is treated as a single entity that happens to consist of characters. </p> <p>In Java, it seems that iterators are only really applied to collections and not to a string. There is no reason why it is this way (near as I can tell - you would probably have to talk to Gosling or the API writers); it appears to be convention or a design decision. Indeed, there is nothing <em>preventing</em> <code>CharSequence</code> from implementing <code>Iterable</code>. </p> <p>That said, you can iterate over the characters in a string like so:</p> <pre><code>for (int i = 0; i &lt; str.length(); i++) { System.out.println(str.charAt(i)); } </code></pre> <p>Or:</p> <pre><code>for(char c : str.toCharArray()) { System.out.println(c); } </code></pre> <p>Or:</p> <pre><code>"Java 8".chars().forEach(System.out::println); </code></pre> <p>Also note that you cannot modify a character of a String in place because Strings are immutable. The mutable companion to a String is StringBuilder (or the older StringBuffer).</p> <p><strong>EDIT</strong></p> <p>To clarify based on the comments on this answer. I'm trying to explain <em>a possible rationale</em> as to why there is no Iterator on a <code>String</code>. I'm not trying to say that it's not possible; indeed I think it would make sense for <code>CharSequence</code> to implement <code>Iterable</code>.</p> <p><code>String</code> provides <code>CharSequence</code>, which, if only conceptually, is different from a <code>String</code>. A <code>String</code> is usually thought of as a single entity, whereas <code>CharSequence</code> is exactly that: a sequence of characters. It would make sense to have an iterator on a sequence of characters (i.e., on <code>CharSequence</code>), but not simply on a <code>String</code> itself.</p> <p>As Foxfire has rightly pointed out in the comments, <code>String</code> implements the <code>CharSequence</code> interface, so type-wise, a <code>String</code> is a <code>CharSequence</code>. Semantically, it seems to me that they are two separate things - I'm probably being pedantic here, but when I think of a <code>String</code> I usually think of it as a single entity that happens to consist of characters. Consider the difference between the sequence of digits <code>1, 2, 3, 4</code> and the number <code>1234</code>. Now consider the difference between the string <code>abcd</code> and the sequence of characters <code>a, b, c, d</code>. I'm trying to point out this difference. </p> <p>In my opinion, asking why <code>String</code> doesn't have an iterator is like asking why <code>Integer</code> doesn't have an iterator so that you can iterate over the individual digits.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload