JDK-5070978 : Allow to use CharSequence in foreach loop
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-07-02
  • Updated: 2015-01-21
  • Resolved: 2006-11-02
Related Reports
Relates :  
Description
Name: js151677			Date: 07/02/2004


A DESCRIPTION OF THE REQUEST :
JSR 201 propose a for loop (foreach) specifically designed for iteration over collections and arrays.
I propose to extend foreach loop to CharSequence.

JUSTIFICATION :
Java 1.5 introduced some new API to improve the way
how java handle character input/output processing
like Scanner or Readable
but doesn't provide support to iterate over strings.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
for(char c:s) {
  System.out.println(c);
}


ACTUAL -
for(int i=0;i<s.length();i++) {
  char c=s.charAt(i);
  System.out.println(c);
}

CUSTOMER SUBMITTED WORKAROUND :
use classical for loop.
(Incident Review ID: 282288) 
======================================================================

Comments
Note that since Java 8, one can write: s.chars().forEach(c -> System.out.println((char)c));
21-01-2015

EVALUATION This is practically a duplicate of 6312085, which requests Iterators as valid Expressions in the enhanced-for loop. The Evaluation there applies here.
02-11-2006