Results 1 to 13 of 13
Thread: Array/Element Question
- 09-23-2010, 02:45 AM #1
Member
- Join Date
- Sep 2010
- Location
- Chicago
- Posts
- 6
- Rep Power
- 0
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
- 09-23-2010, 04:24 AM #2
It looks like you have answered your own questions.
Or do you have further questions?
- 09-23-2010, 04:59 AM #3
Member
- Join Date
- Sep 2010
- Location
- Chicago
- Posts
- 6
- Rep Power
- 0
- 09-23-2010, 07:55 AM #4
- 09-23-2010, 08:50 AM #5
Member
- Join Date
- Sep 2010
- Location
- Chicago
- Posts
- 6
- Rep Power
- 0
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!
- 09-23-2010, 09:06 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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
- 09-23-2010, 09:34 AM #7
Member
- Join Date
- Sep 2010
- Location
- Chicago
- Posts
- 6
- Rep Power
- 0
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?
- 09-23-2010, 01:22 PM #8
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.
- 09-23-2010, 10:53 PM #9
Member
- Join Date
- Sep 2010
- Location
- Chicago
- Posts
- 6
- Rep Power
- 0
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!
- 09-23-2010, 11:11 PM #10
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:
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?Create a String called 'current', and make it equal to the 'count' element of the String array 'phrase'
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?
- 09-23-2010, 11:25 PM #11
Member
- Join Date
- Sep 2010
- Location
- Chicago
- Posts
- 6
- Rep Power
- 0
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;
?
- 09-23-2010, 11:49 PM #12
Going back to my example, count is the number on the piece of paper, NOT the contents of the mailbox.
- 09-24-2010, 01:13 AM #13
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
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.)
In each visit within the for-loop they perform theJava 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 }
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
-
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
Unique element in an array
By revathi17 in forum New To JavaReplies: 2Last Post: 12-31-2007, 08:44 AM -
Max element in an Array
By mew in forum New To JavaReplies: 5Last Post: 12-03-2007, 05:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks