• Post Reply Bookmark Topic Watch Topic
  • New Topic

String indexing method help  RSS feed

 
Fritz Haller
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello forum members. I'm a Java beginner, writing some software implementation for my academic research, but I'm not a programmer, so I could really use your help in order to improve in programming.
I'm currently writing a program where I need to have a function which indexes some String elements from a Vector list. It should work like this: If strings are repeating, they should get extension 0001, 0002, etc.
But if they exist in a list just once they should have 0001.
So if the elements of the list are for example:

F1_C1
F1_C1
F2_C1
F3_C1
F3_C1
...

output should be:

F1_C1_0001
F1_C1_0002
F2_C1_0001
F3_C1_0001
F3_C1_0002
...

This is how I managed to do it, but I think this is too long, with too many iterations.



Do you have some suggestions for a better, cleaner code which solves this problem?

Thank you very much,
Fritz
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have inserted all the list elements into a Map such that the key is the string and the value is the count of the occurrence of that string.

e.g.



then I would have simply iterated over this prepared map to get the desired output.

e.g.


 
Fritz Haller
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works perfectly! I never used maps before, but now I see how good they can be for problems like this.
Thank you very much for your help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!