Results 1 to 13 of 13
  1. #1
    Polymer is offline Member
    Join Date
    Sep 2010
    Location
    Chicago
    Posts
    6
    Rep Power
    0

    Default Array/Element Question

    Trying to understand the mechanics of some code in this Array exercise.

    What is the plain english translation of line 6?

    "Create a String called 'current', and make it equal to the 'count' element of the String array 'phrase' "?

    What about line 7?

    "Create a char array called 'letters' and make it's value equal to the String 'current'...."

    Thanks in advance!

    Java Code:
    class Wheel {
        public static void main(String[] args) {
            String phrase[] = {"Robot", "Alamo", "Cigarette", "Sugar Cane"};
            int[] letterCount = new int[26];
            for (int count = 0; count < phrase.length; count++) { 
                String current = phrase[count]; 
                char[] letters = current.toCharArray();
                for (int count2 = 0; count2 <letters.length; count2++) {
                    char lett = letters[count2];
                    if ( (lett >= 'A') & (lett <='Z')) {
                        letterCount[lett - 'A']++;
                    }
                    }
                }
                for (char count = 'A'; count <= 'Z'; count++) {
                    System.out.print(count + ": " +
                        letterCount[count - 'A'] +
                        " ");
                }
                System.out.println();
        }
    }
    Last edited by Polymer; 09-23-2010 at 04:57 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    It looks like you have answered your own questions.
    Or do you have further questions?

  3. #3
    Polymer is offline Member
    Join Date
    Sep 2010
    Location
    Chicago
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    It looks like you have answered your own questions.
    Or do you have further questions?

    Well, I am confused about whats going on after the '=' on line 6 and line 7.
    Last edited by Polymer; 09-23-2010 at 05:31 AM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

  5. #5
    Polymer is offline Member
    Join Date
    Sep 2010
    Location
    Chicago
    Posts
    6
    Rep Power
    0

    Default

    Hey Darryl, thanks for the links - these cleared up a lot of other questions I had.

    Unless I overlooked I didnt see anything directly relevant to line 6 however.

    I certainly appreciate the senior members "teaching us to fish" vs just "feeding us", but after reading through all the supplied material I still dont understand the plain english that is occurring on line 6 after '=', which is 'phrase[count];' .

    I understand we are doing something with the String array 'phrase' and the int 'count' that we defined in the 'for' statement above it, but I do not understand their relationship in this statement.

    I'm sorry if it is a basic question, I've never programmed before and this is like my 3rd day into teaching myself Java. Can anyone explain this line to me like you might a child?

    Thanks again in advance!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Arrays are like a block of houses; every house has a unique number, starting at zero, so 0, 1, 2, ... etc. If the street is named 'phrase' then, say, 'phrase[2]' is the house with number 2. If the number is 'count' then 'phrase[count]' is the house with number 'count'. The value 'count' is called the index and of course 'phrase' isn't a row of houses but it is a row of Strings, so 'phrase[count]' is the 'count-th' String in the row (array).

    kind regards,

    Jos

  7. #7
    Polymer is offline Member
    Join Date
    Sep 2010
    Location
    Chicago
    Posts
    6
    Rep Power
    0

    Default

    Thanks JosAH. I understand that st/house idea, at least conceptually.

    Look, line 5 says to me:

    for (int count = 0; count < phrase.length; count++) {

    "in a for loop, create an int called 'count' and give it the value of zero; so long as count is < the number of variables stored in the phrase array (4); add 1 to 'count' "

    Can someone transliterate lines 6 & 7 in this fashion?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    variables represent computer memory addresses. An array uses memory in a range of memory addresses.
    The address of the first element is at relative location 0. Say each element in phrase is 4 bytes long. Then address of the next element in the array is the beginning address + 4. The next one after that at +8 etc to the end of the array.
    So the address of the element phase[count] is the address of phase + count *the length of an element.

  9. #9
    Polymer is offline Member
    Join Date
    Sep 2010
    Location
    Chicago
    Posts
    6
    Rep Power
    0

    Default

    phrase + count*length of the element?

    phrase.length= 4, but phrase is itself is a String. I can't add or multiple an int with a String.

    Is anyone capable of dumbing this down a little? Just looking for a plain and simple plain english explanation of line 6 and 7. Narrate it for me.

    6: "Create a String called 'current' and make it equal to (whatever is going on east of the '='.";
    7: "Create a character array called 'letters' and make it equal to (whatever is going on east of the '='.;

    Thank you for your help!

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    Sorry, there isn't a 'more dumb' way of explaining then has been used.
    This is the best of the explanations, the one you gave:
    Create a String called 'current', and make it equal to the 'count' element of the String array 'phrase'
    What about this way: consider that an array is a row of mailboxes. If I ask you to look in the 4th mailbox for a String how would you find the one I'm refering to?
    What if instead of asking the for 4th mailbox, I gave you a piece of paper with the number 4 written on it and asked you to go to the mailbox referred to by the number on the piece of paper. Could you find it?

  11. #11
    Polymer is offline Member
    Join Date
    Sep 2010
    Location
    Chicago
    Posts
    6
    Rep Power
    0

    Default

    Yes, I think I could - thats a good analogy Norm.

    I'm glad I was on the right track initially. Recall when I originally proposed "Create a String called 'current', and make it equal to the 'count' element of the String array 'phrase'?" it was in the form of a question.

    I guess where I am still confused here is:

    5: for (int count = 0; count < phrase.length; count++) {
    6: String current = phrase[count];

    Why do I need to reference the phrase array at all on line 6? To get the numerical value bring tested and modified by the for loop in line 5, why wouldn't I just say:

    6: String current = count;
    ?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    Going back to my example, count is the number on the piece of paper, NOT the contents of the mailbox.

  13. #13
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    One of the qualities of the String variable "current"
    is that it is not permanent. It is a temporary
    workspace, along with the "letters" character array.
    Beyond the scope of the line 5 for-loop, they cease
    to exist.

    (Notice they don't show up beyond line 14, where the
    line 5 for-loop ends.)


    Java Code:
    1   class Wheel {
    2       public static void main(String[] args) {
    3           String phrase[] = {"Robot", "Alamo", "Cigarette", "Sugar Cane"};
    4           int[] letterCount = new int[26];
    5           for (int count = 0; count < phrase.length; count++) { 
    6               String current = phrase[count]; 
    7               char[] letters = current.toCharArray();
    8               for (int count2 = 0; count2 <letters.length; count2++) {
    9                   char lett = letters[count2];
    10                  if ( (lett >= 'A') & (lett <='Z')) {
    11                      letterCount[lett - 'A']++;
    12                  }
    13              }
    14          }          
    15          for (char count = 'A'; count <= 'Z'; count++) {
    16              System.out.print(count + ": " +
    17                  letterCount[count - 'A'] +
    18                  " ");
    19          }
    20          System.out.println();
    21      }
    22  }
    In each visit within the for-loop they perform the
    same ol' same ol'..

    1st visit:
    current gets "Robot"
    letters[] gets {R, o, b, o, t}

    1st interation:
    current gets "Alamo"
    letters[] gets {A, l, a, m, o}

    2nd interation:
    current gets "Cigarette"
    letters[] gets {C,i,g,a,r,e,t,t,e}

    3rd interation:
    current gets "Sugar Cane"
    letters[] gets {S,u,g,a,r, ,C,a,n,e}


    Work at lines 6 and 7 ends after the third
    iteration because the for-loop on line 5
    was programmed to run for the length of the
    phrase[] String array (it has 4 Strings in it).

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  2. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  3. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  4. Unique element in an array
    By revathi17 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 08:44 AM
  5. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 05:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •