Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Linear comparison algorithm

 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming you posted before I submitted my last revision. I also changed the Object arrays to int arrays in the driver. I am now getting the correct elements just not enough comparisons.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally figured it out.... But one last thing. Any ideas on how I can include duplicate elements? I've sorted the arrays and managed to find all common elements, but I want multiple values of a common element.
Such as, 28, which appears in only two sets but should be printed twice in the common set.






Ouput:

The following elements were common among all arrays:

[[1, 2, 3, 4, 5, 16, 22, 23, 25, 28]]

Additionally, there were a total of 40 comparisons made between the arrays.

[Ljava.lang.Comparable;@3ee284 <<<<<<---- not sure why this though??
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"hashset"....And from your suggestions, though I had no idea how to implement it at first. (I'm referring to the comments I left in the code).
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Mcloud wrote: . . .
// why do I keep getting [Ljava.lang.Comparable;@3ee284 . . .

[Ljava.lang.Comparable;@3ee284 <<<<<<---- not sure why this though??
Because the only method which an array overrides is clone(). You are using Object#toString inherited unchanged.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. So cast it somehow?
 
Winston Gutkowski
Bartender
Pie
Posts: 10072
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Mcloud wrote:Finally figured it out....

Well done. And I notice you've now included a HashSet, in which case you might be interested in its retailAll() method - it'll reduce your code even more.

But one last thing. Any ideas on how I can include duplicate elements? I've sorted the arrays and managed to find all common elements, but I want multiple values of a common element. Such as, 28, which appears in only two sets but should be printed twice in the common set.

Hunh? I thought you were only interested in elements that were common to all arrays. When I was talking about duplicates, I meant including two copies of an element in your result only if ALL arrays have at least two of them.
And the only way I know to do that is:
1. Count occurrences of each distinct value in each array.
2. Eliminate values where any of the counts is 0;
3. If the minimum count is > 1, include that number of copies in the output.
And for that, you will almost certainly need a Map (unless you want to write a lot of unnecessary code).

If that's not what you want, I think you'll have to describe your requirements a bit more clearly.

I see. So cast it somehow?

No, no, no. In fact, your program has far too many casts in it already (you could actually write it without a single one). Casts are evil, and to be avoided at all costs.

Winston

PS: Please remember what I said about DontWriteLongLines. I've broken yours up again, but this is the last time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic