This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Array Intersection

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, as part of a program I must write I have a method void takeIntersectionWith(Object other)which takes an Array object and compares it to another and finds the intersection point.For example, if one bag contains {1,1,3,3,4} and the other bag is {1,1,1,3,7} the intersection is {1,1,3}. The method can't change the other bag. I'm not really sure where to start on this,
Basically all I know is that I'll need to cycle through both arrays and compare the values, but I'm kinda unclear on how to do this. Any advice would be greatly appreciated.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can sort them in ascending order you can use one of my oldest favorites:

"handle match" is where you'll deal with intersections. It would be easiest to create a new third bag (manned by I Don't Know) and put the intersection in there than to munge one of the originals by removing non-intersection items.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW: Norville, one of the sherrifs will doubtless ask you (and the rest of the gang to change your user name less obviously fictional.


[ September 22, 2006: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!