Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: How to check itersection of objects in on collection

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default How to check itersection of objects in on collection

    Hi to all !
    Im looking for a solution to my problem.
    Dont know how to check itersection of rects from objects in one List.
    Example: We have a Object.class;The Object has a simple method
    Rectangle getRect(){return new Rectangle(x,y,10,10)}
    We create a new List; Adding some Objects to it.
    Now the problem, how to check does the Rectangles in one list itersecting each other or not ?
    I try to create an iterator the a loop the an if statement
    Iterator<Object> i = list.iterator();
    while (i.hasNext()){
    Object o = i.next();
    if (o.getRect().itersects())
    {
    //Some code
    }
    }
    Well i dont know what to put in the intersects() method;
    Adwises ?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to check itersection of objects in on collection

    Well i dont know what to put in the intersects() method;
    Read the API for this method - it can accept a variety of parameters, one of which is another Rectangle object. So pass the other Rectangle you wish to compare.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to check itersection of objects in on collection

    Look at my example code , ive allready the itersects and of course i know that i need an other object.
    Ive asked about objects in one list. According to my example and using you solution it mean that ive to this if (o.getRect().itersects(o.getRect())) and this is not right because it checks itself.
    All objects are in one collection, how to chekc them ? what should be in the intersects()?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to check itersection of objects in on collection

    You've been given help already, I suggest your reply with a little more gratitude.

    The intersects() method takes an argument. You need to figure out what that argument should be. What it should be depends on your end goal, which only you know.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    copeg (July 10th, 2012)

  6. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to check itersection of objects in on collection

    No thats not the help, becasue ive asked not about the functionality of the itersect() method.
    Okay and here is a a simple solution of my problem i found.
    Ive to use 2 itertators
    Iterator<Object> i = list.iterator();
    Iterator<Object> i = list.iterator();
    while (i.hasNext()){
          while(j.hasNext()){
                Object o1 = i.next();
                Object o2 = j.next();
                if (o1.getRect().itersects(o2.getRect()))
                 {
                 //Some code
                 }
          }
    }
    And it works! Hope this will some else.
    And some help from others
    this is more right becaouse it cheks that objects are not itersectin them self
    for( int i = 0; list.length; i++)
    {
      Rectangle current = list[i].getRect();
      for( int j = 0; list.length; j++ )
      {
         Rectangle compare = list[j];
         if( !current.equals(compare)) //you could also check to see if i!=j
           if( current.intersects(compare) )
              System.out.println( "They intersect!");
      }
    }
    Last edited by Eldarmk2; July 10th, 2012 at 09:37 AM.

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to check itersection of objects in on collection

    That was exactly what copeg told you, to take a closer look at the intersection() method, which you were using incorrectly. Actually, I predict you'll have quite a few problems with that code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to check itersection of objects in on collection

    i know how the intersects90 work since my first java book
    The problem was that i dindt think about 2 iterators in one list.
    This code should now be correct
    Iterator<Object> i = list.iterator();
    Iterator<Object> j = list.iterator();
    while (i.hasNext()){
          Object o1 = i.hasNext();
          while(j.hasNext()){
                Object o2 = j.next();
                if (o1.getRect().itersects(o2.getRect()))
                 {
                 //Some code
                 }
          }
    }

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to check itersection of objects in on collection

    That still is not correct. Aren't you testing this out as you go?

    Also, did you really use Object as a class name?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to check itersection of objects in on collection

    Quote Originally Posted by Eldarmk2
    becasue ive asked not about the functionality of the itersect() method
    I beg to differ...

    Quote Originally Posted by Eldarmk2
    Well i dont know what to put in the intersects() method
    Getting answers is all about phrasing questions. Phrase them in a way that doesn't correlate with your problem, then the answer you receive will not help with your problem.

  11. #10
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to check itersection of objects in on collection

    No my class is caled shape

  12. #11
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to check itersection of objects in on collection

    Quote Originally Posted by copeg View Post
    I beg to differ...



    Getting answers is all about phrasing questions. Phrase them in a way that doesn't correlate with your problem, then the answer you receive will not help with your problem.
    Ok thank you

  13. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to check itersection of objects in on collection

    Recommended reading: Coding Horror: Rubber Duck Problem Solving
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. collection problem
    By freakycoder in forum Collections and Generics
    Replies: 13
    Last Post: June 26th, 2012, 11:05 AM
  2. When use Collection Framework?
    By hexwind in forum Java Theory & Questions
    Replies: 7
    Last Post: July 31st, 2011, 04:19 PM
  3. Synchronized Collection
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: March 29th, 2011, 12:09 AM
  4. Looking for Collection class to do first if first out
    By snytkine in forum Collections and Generics
    Replies: 5
    Last Post: October 7th, 2010, 09:18 PM
  5. Garbage Collection?
    By kalees in forum Collections and Generics
    Replies: 6
    Last Post: September 23rd, 2009, 03:07 AM