This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Trying to figure out a get method using an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to figure out a get method using an array" Watch "Trying to figure out a get method using an array" New topic
Author

Trying to figure out a get method using an array

Tyler Tallman
Greenhorn

Joined: Oct 04, 2012
Posts: 20
So I have this constructor that creates an inventory of the individual letters that are passed through it, and then keeps track of the total amount of letters that have been passed. I keep track of the individual letters by incrementing it's respective spot in a counter array, so if there are two a's passed, then there should be a two in the zero spot, if there are three d's, then there should be a three in the three spot.

My problem is trying to access this through a get method that I'm using. I have it looping through a list of the alphabet and I know that it is wrong, since all it is doing is telling me that there is one of each letter in that list. I want to use my counter array from the constructor to get my values, but I am having trouble figuring out how I am supposed to write the code.



The prints are so I can make sure that it is compiling properly. What I am looking at is my for loop in the get method, specifically how can I loop through my counter array from the constructor so I can get the proper values. Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

So, index 0 corresonds to the number of 'A's, 1 to the number of 'B's and so on?

So if you have "AAA B DD" your array will look like {3, 1, 0, 2, ...} yes?

And, given that the array is already populated, you want public int get(char c) to return the number of occurrences of that character? So if I call get('A') it returns 3 and get('D') returns 2?

If this is all correct, then there's no need for any looping in the get method. Think of how you'd do it "by hand".

'A' --> arr[0]
'B' --> arr[1]
...
'Z' --> arr[25]

See a pattern there? Also, keep in mind that char is a numerical type, and you can perform arithmetic on chars (though you have to be careful and it's not really advisable in the general sense), and the numeric values of 'A' through 'Z' are consecutive.

Put this together, and your get() method becomes a one-liner.

Also, your list variable is unnecessary, and you can correspondingly trim your constructor down.

HOWEVER, since you've put the effort into this approach and got the constructor working the way you want (I assume--haven't actually looked at it), and you want to stick with it, then the same basic approach you used in the c'tor can be used in the method.

1. Iterate over list looking for a letter that matches the parameter passed to get().

2. Note the index of that letter once you've found it.

3. Retrieve the value from the corresponding index of your count array.

Tyler Tallman
Greenhorn

Joined: Oct 04, 2012
Posts: 20
Wow that was the perfect way to put it, I changed my code and the get method works perfectly now. Thanks a ton!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Cool. Glad you got it sorted out!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to figure out a get method using an array
 
Similar Threads
More Array sorting trouble
Desperately Need Help
Problem running a program practicing with Arrays and Classes
Need help with creating a random array.
a 2D array in a class constructot