Why does the foreach statement not change the element value?

前端 未结 6 1222

How come the following prints boss and not bass?

String boss = \"boss\";
char[] array = boss.toCharArray();

for(char c : array)
{
 if (c== \'o\')
     c =          


        
6条回答
  •  我在风中等你
    2020-11-22 01:08

    Changes applied in 'for each' loop are made just inside her body (that's because values are copied, not referentions). To work on referentions use 'for' loop.

提交回复
热议问题