java - why no type mismatch error in string split method when run in loop? -
this question has answer here:
- what colon mean in java? 5 answers
i have string handling related question
split() - return type string[]
in loop storing split value in string literal
for (string retval: str.split("-"))
why doesn't give type mismatch error in code below?
string str1 = "abfg-hjddh-jdj"; string str = str1.split("-");
string str = str1.split("-");
gives error because split returns array, correct syntax is:
string[] str = str1.split("-");
in for-each
loop
for (string retval : str.split("-"))
for each loop :
indicates iterating array, collection or list of strings, no error trhown
examples:
for (int retval : string.split("-")) // error,
arraylist<books> books; (book book : books) // correct
set<integer> integers; (integer mint : integers) // correct (string mint : integers) // incorrect!!!
last not least: variables in java should start lowercase
, please check code conventions.
Comments
Post a Comment