5 Replies - 214 Views - Last Post: 02 April 2013 - 04:54 PM Rate Topic: -----

#1 Pmike86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

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

Posted 02 April 2013 - 03:54 PM

Please give us some context:

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?
Was This Post Helpful? 0
  • +
  • -

#3 Pmike86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 401
  • View blog
  • Posts: 998
  • 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.
Was This Post Helpful? 1
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • 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.
Was This Post Helpful? 1
  • +
  • -

#6 Pmike86  Icon User is offline

  • New D.I.C Head

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

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

Posted 02 April 2013 - 04:54 PM

View PostGregBrannon, 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1