• Post Reply Bookmark Topic Watch Topic
  • New Topic

Detect arrays with the same values  RSS feed

 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What code could I use for this problem: Search a String array for two array indexes that have the same value (possibly +/- whitespace) and then getting rid of both similar array indexes.
 
Brian Burress
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With this in mind (read me please). What have you tried?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't. An array has a fixed size, so once it is created with (say) ten elements, it will always have ten elements. Now, you can set some of those spots to be 'null', but they are still there. And there is no easy way to scoot the remaining values down one spot, thus putting the leftover values at spots 0-X, and the nulls at x+1 to length().

So, assuming you need a NEW array with only the elements that didn't repeat in the original array, I'd suggest you do this...

Get paper, pencil, and eraser (or their digital equivalent). Write out longhand, in English, how YOU would do it if you were handed a piece of paper with a list of strings and you couldn't erase anything from that piece of paper. Then revise it, making each step simpler each time, until you have VERY simple, clear directions. THEN you start writing code, but NOT before.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I realize a new array will be required. My main question was whether or not there's a "built-in" way to search array indexes. Guess not. So I guess I just do it like such. Search each array index for example[x]. If no search finds anything, add the index to example1[x]. If it does, skip over it and move on.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:Yes, I realize a new array will be required. My main question was whether or not there's a "built-in" way to search array indexes. Guess not.

Sure there is: a for loop. Or did you have something else in mind?

Winston
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the other hand a List does have an "indexOf(Object)" method. You've already found that an array is problematic for your requirements because of its fixed size. But if you used a List, then you could not only remove entries easily, you can also search for them easily. What's not to like?
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you use List you can do these manupulations easily and if you only want to use array then you can just copy that List to your array variable by toArray() method.
 
Noam Ingalls
Ranch Hand
Posts: 60
Chrome Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely support the List implementation-- might one suggest an ArrayList? You can copy from your Array to the ArrayList using the methods listed in the API.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!