# for( char c: a.toCharArray) help with logic

Page 1 of 1

## 5 Replies - 852 Views - Last Post: 02 April 2013 - 04:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=317484&amp;s=5280f9187f55d0c5215dc9005398df29&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Pmike86

Reputation: 0
• Posts: 11
• Joined: 01-March 13

# for( char c: a.toCharArray) help with logic

Posted 02 April 2013 - 03:51 PM

Can someone please break this down for me and explain to me what this is doing? I understand we have a for loop.. It also seems as though we are converting something to a char array.. other than that i am not really sure what its doing. Thank you.

here is a larger example: - I merely need help with the logic of the first for loop.

```  String newString = ""; //empty string as placeholder
for(char c : a.toCharArray()){
boolean result = false;
for(int i=0; i<b.length(); i++){ //loop through does letter = charAt (i) of string?
if(c == b.charAt(i)){
result = true;
i = b.length();
}
}
if(result)
newString += c;
else
newString += "-";

```

Is This A Good Question/Topic? 0

## Replies To: for( char c: a.toCharArray) help with logic

### #2 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: for( char c: a.toCharArray) help with logic

Posted 02 April 2013 - 03:54 PM

What is it?

What's it supposed to do?

Where'd you get it? (Can you provide a link or reference?)

Why do you want to know?

### #3 Pmike86

Reputation: 0
• Posts: 11
• Joined: 01-March 13

## Re: for( char c: a.toCharArray) help with logic

Posted 02 April 2013 - 04:33 PM

I wrote it up myself - it was during class today but I never got a full explaination as to what exactly the for( char c = blah blah blah) is actually doing here...

in the code itself: I am passing a parameter named b which is a CharSequence. I am checking against every position in the array as I increment i. and returning a new "result" which is going to be my new String.

What I am trying to figure out is how a for loop code inside. Obviously c is never incremented so I would think it would be an infinite loop... It's just something I am trying to understand better for class.

Sorry i forgot to include this... I am passing a string named "a".. converting it to a char array by the looks of it... thats kinda where I am confused and as to how this is inside a for loop really baffles me

### #4 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: for( char c: a.toCharArray) help with logic

Posted 02 April 2013 - 04:50 PM

If you are talking about 'for(char c: a.toCharArray())' that is known as an enhanced for loop.

As you say, your string will be split into an array of chars and then run through a loop until the loop reaches the end of the array.

### #5 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: for( char c: a.toCharArray) help with logic

Posted 02 April 2013 - 04:51 PM

Thanks, that helps.

What's missing is the definition of 'a'? But without that, the for loop that's confusing is called (officially) the "enhanced" for loop or, commonly, the "for each" loop. The second name makes it easier to read and understand. Read it as:

for(char c : a.toCharArray())

"for each character c in the collection a," etc. etc. . .

'a' is not defined in what you've posted, but I assume it's a String object which is being converted to a character array.

After that, b is another unknown (were you drousy?), so the logic continues to fall apart. My comments may not be quite right, but what the code does is look for any character in b that also exists in a and collects those characters in newString. How what you've written might be used (b still undefined):
```String a = "this is a string";

//empty string as placeholder
String newString = "";

for ( char c : a.toCharArray() )
{
boolean result = false;

//iterate b to compare to each character in a
for ( int i = 0 ; i < b.length() ; i++)
{
// if the current a character is the same as
// the character in b
if ( c == b.charAt(i) )
{
result = true;

// set the location of the equality
// this terminates the iteration of b
// so that duplicates are not collected
// 'break' would do the same thing
i = b.length();
}
}

// collect the characters that are the same
if( result )
{
newString += c;
}
else
{
newString += "-";
}
}
```

Hope this helps.

### #6 Pmike86

Reputation: 0
• Posts: 11
• Joined: 01-March 13

## Re: for( char c: a.toCharArray) help with logic

Posted 02 April 2013 - 04:54 PM

GregBrannon, on 02 April 2013 - 04:51 PM, said:

Thanks, that helps.

What's missing is the definition of 'a'? But without that, the for loop that's confusing is called (officially) the "enhanced" for loop or, commonly, the "for each" loop. The second name makes it easier to read and understand. Read it as:

for(char c : a.toCharArray())

"for each character c in the collection a," etc. etc. . .

'a' is not defined in what you've posted, but I assume it's a String object which is being converted to a character array.

After that, b is another unknown (were you drousy?), so the logic continues to fall apart. My comments may not be quite right, but what the code does is look for any character in b that also exists in a and collects those characters in newString. How what you've written might be used (b still undefined):
```String a = "this is a string";

//empty string as placeholder
String newString = "";

for ( char c : a.toCharArray() )
{
boolean result = false;

//iterate b to compare to each character in a
for ( int i = 0 ; i < b.length() ; i++)
{
// if the current a character is the same as
// the character in b
if ( c == b.charAt(i) )
{
result = true;

// set the location of the equality
// this terminates the iteration of b
// so that duplicates are not collected
// 'break' would do the same thing
i = b.length();
}
}

// collect the characters that are the same
if( result )
{
newString += c;
}
else
{
newString += "-";
}
}
```

Hope this helps.

This is actually exactly what I was looking for. Thank you for a well explained definition of this. Sorry I didnt identify my "a" and "b" properly but you nailed it on the head and it def makes a lot more sense to me now!