• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList: Find all Indexes of an Occurrence  RSS feed

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to read a list of people and show at what indexes that person appears. Below is what I have so far (I know it's wrong), but I wondering what I can do make this do what I want. I reading on Array Lists right now, so I think toArray() is what I'm looking for, but I'm not sure.

 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam,
I don't think you'll be better off with an array than an ArrayList to find all occurrences. I can't think of a better way than looping through the ArrayList and checking each value.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:I am trying to read a list of people and show at what indexes that person appears. Below is what I have so far (I know it's wrong), but I wondering what I can do make this do what I want. I reading on Array Lists right now, so I think toArray() is what I'm looking for, but I'm not sure.

I'll put you out of your misery: No it isn't.

Think about what you're trying to do, and write it down on paper BEFORE you start coding.

So - First question: What ARE you trying to do? (Hint: you've already written it in your post)

Forget Java for a moment. What if you handed your list - let's say a bunch of numbered cards with people's names written on them - to a 10-year old kid and asked him to find a particular person, what would you expect HIM to do? Now, write that procedure down in detail and in English (or your native language) before you write one line of Java code.

There's no great mystery about programming once you understand the problem; but solutions will not just magically appear because you start coding. In fact, they're far less likely to appear if you start coding without thinking first.

Second question: Have you ever used ArrayList before? If not, you should probably read the tutorials before you go any further. If you have (and once you've completed the task above), make sure you have a copy of the API docs for java.util.ArrayList (←click) open on your desktop when you start coding.

(BIG Hint: Once you HAVE read the API docs, you'll probably discover that you can do what you want in 1 line of code - well, maybe a few more to get ALL indexes )

HIH

Winston
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In more detail, here's what I want to do.

1. Loop through the entire array.
2. As I'm looping through, look for a certain value.
3. If that value occurs, print out at what index the value occurs.
4. Once I've looped through to the end, I will see all the indexes where that value appears.

Below is the my adjusted code, which still isn't working, but it's where I am at the moment.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:Below is the my adjusted code, which still isn't working, but it's where I am at the moment.

Well, it looks MUCH better (and much closer to a solution) except that you're mixing up concepts.

What does indexOf() DO? And, more importantly, what does it take as a parameter?

What is your for loop doing?

You're not far off, you just need to probably look at the docs a bit more and see what other method(s) you might use (and BTW, well done for finding indexOf(), because it's definitely part of a good solution).

Winston

PS: I should add that there are SEVERAL ways to do this, and none of them is "right". The "rightest" will probably be the one you find easiest to understand.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
What does indexOf() DO? And, more importantly, what does it take as a parameter?

What is your for loop doing?


indexOf() is supposed take in the index of i in the array.

My for loop takes me through the entire array.

On a side note, this is for an assignment, and we're required to use .equals as part of this code.

Edit: I'm looking at iterator(), but still trying to figure out how I'd stick it in, assuming it would be useful
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:indexOf() is supposed take in the index of i in the array.

No it isn't. Look at the docs again. Carefully.

My for loop takes me through the entire array.

Yup. And more specifically, it takes you through element by element.

On a side note, this is for an assignment, and we're required to use .equals as part of this code.

Ah. Well, in that case maybe indexOf() isn't what you want (because it does the equals() bit for you). So, what else do you think you might use? Remember: on each cycle of the loop, you already have the index.

Edit: I'm looking at iterator(), but still trying to figure out how I'd stick it in, assuming it would be useful

It could be; but for now I'd stay on the lines you were already were. Like I said, you're very close; you just need one final piece of the puzzle.

When you've got THAT working - and if you still have time - then might be the time to think about iterators.

Winston
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Prints ".....at index false." This is the first time I got it to print something. However the instructions specifically say "Use the method .equals to compare the entry to "Mike Trout" then print out the index number if it is equal.">
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:Prints ".....at index false." This is the first time I got it to print something. However the instructions specifically say "Use the method .equals to compare the entry to "Mike Trout" then print out the index number if it is equal."

Right, but you have to use it on the right thing. There's no point in simply "trying things out" blindly, because the likelyhood is: it won't work. StopCoding (←click) and think about what you need to do.

What is 'players'? Is it a String? So, what do you think will happen when you try to compare it with a String?

Also: What does contains() do? Write down a description of exactly what the method does (copy it down from the docs if you need to).
Now: Do you really think your need to run it for every element in your list?

Think; don't code. You're a whisker away from the solution - or rather, you were; I notice you've now edited your last post 4 times - but right now you're just flailing around instead of doing what got you this far.

Here's a tip for you: How do you get a single element, at a particular index, from an ArrayList?

Winston
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Players" is an instance of my array list. contains() "Returns true if this list contains the specified element" so I guess that's not what I'm looking for. As for your question, I do have this.


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:"Players" is an instance of my array list. contains() "Returns true if this list contains the specified element" so I guess that's not what I'm looking for. As for you questions, I do have this.

Right. NOW we're getting somewhere. So, how do you think you can tie THAT together with equals() to get what you want?

Winston
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this now, but it's only printing the first occurrence of course.

>
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:Might be getting somewhere....

Nope. You're still trying to compare 'players', which is an ArrayList, with "Mike Trout", which is a String; and that will never work (or rather, it will ALWAYS return false).

However, I'm afraid it's time for my bed (3:30 AM here), so I'll leave you with it.

Think, think, think. Man, you're so close...you just need to 'see' that last hurdle.

Winston
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it's something like this? Maybe I need to have something like "If the index at i". Need to figure out how to do that...


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:Maybe it's something like this? Maybe I need to have something like "If the index at i". Need to figure out how to do that...

Bingo. Now you just have to work out what "SOMETHING" is (BIG HINT: you've already written it).

Winston
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this means I'm closer. It's printing out two statements referencing index -1.

>
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are REALLY close. In the println, why are you calling indexOf? You already know the right index from looping through.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:You are REALLY close. In the println, why are you calling indexOf? You already know the right index from looping through.

And that goes back to thinking, Sam - and reading - rather than just "trying out" code at random. You really must try to get into the habit; otherwise you're likely to be frustrated a lot.

What does indexOf() DO? Go back to the docs and copy down the explanation. And if you don't understand it: Ask.

However, Jeanne is absolutely right. If you were a whisker away before, you're a quark away now.

Winston
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
indexOf() returns the index of the first occurrence of something. Would I be correct in saying that everything I have is right except for the indexOf()?
 
Sam Pauken
Ranch Hand
Posts: 84
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it! Thanks for all your help!

 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done It would have been easier had the indexOf() method been overloaded to take a starting index.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you show me what that would look like?
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can of course subclass ArrayList and add that method.
Such indexOf(T, int) methods tend to include the stated index in the search, so if you find something at 5, a search from index 5 will find it again at index 5 and then it will search again at index 5 and find it at index 5 and ...
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note I have used a hypothetical and non‑existent method.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is a similar problem, but just wanted to post this. Similarly, we want to find all the people whose name starts with "J". Do you think it would be easier to search for the character at 0 for an index? I really can't just search for the letter "J" since a last name might start with that. I know I cannot use charAt() since these are ArrayLists.

Here's my logic in words so far.

1. Loop through the entire array.
2. As I loop through, look at the character at 0.
3. If that character is "J", print out the person's name.

 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use charAt on the elements in the List.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose I'm doing it wrong then, since I get a red X for charAt(). It says the method is undefined.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:I suppose I'm doing it wrong then, since I get a red X for charAt(). It says the method is undefined.

Because you're using it on the wrong type. One more time: What is 'players'? And what type is charAt() defined for?

Tip: Stop using great strings of methods; it's clearly confusing you. Call one method at a time and store the result in a field; then call the next method on that field, and store its result....and so on. Hopefully it'll make what's happening more obvious to you.

And BTW, String also has a startsWith() method, which might be a bit clearer.

Winston

PS: Well done for getting the previous one solved.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got this one too. Thank you all for your help.

>
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:Got this one too. Thank you all for your help.

Well done. But you actually have some redundancy in there. On the other hand:doesn't.

Yet another reason for not using long strings of methods.

Another tip for you: Space your code out a bit. It's not a competition to see how short you can make it.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!