XNA Creators Club Online
Page 1 of 1 (9 items)
Sort Posts: Previous Next

Removing int[] from lists

Last post 11/22/2009 11:52 AM by Jason Guthrie. 8 replies.
  • 11/18/2009 1:57 PM

    Removing int[] from lists

    Having a really strange error from the framework here. I have a static list full of int[] called laser targets. Adding and reading from this list is fine but when i try and remove items from the list nothing happens. what i am trying to do in my call is similar to this:

    Spawner.laserTargets.Remove(new int[]{0,0});

    but when the code runs it doesnt remove the int[] and it is definitely there. I have also tried:

    int[] temp = new int[]{0,0};
    Spawner.laserTargets.Remove(temp);

    and nothing happens. Anyone any ideas why? My project is on a standstill until i can solve this :(
  • 11/18/2009 2:01 PM In reply to

    Re: Removing int[] from lists

    Answer
    Reply Quote
    Becuase the {0,0} array that is in the list, and the new int[]{0,0} that you create in your code are completely different objects.

    To remove an object from the list, you need to have a reference to that exact object (or know its exact position in the list).

  • 11/18/2009 2:02 PM In reply to

    Re: Removing int[] from lists

    Answer
    Reply Quote
    You're trying to remove a new item, which doesn't exist in the list. Try:

    int[] temp = Spawner.laserTargets[some index here];
    Spawner.laserTargets.Remove(temp);

    or:

    Spawner.laserTargets.RemoveAt(some index here);
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 11/18/2009 2:13 PM In reply to

    Re: Removing int[] from lists

    Ahh thanks for the responses guys i was guessing at something similar. I did try removeAt before, which worked perfectly, but the list is edited constantly so any index i store becomes irrelevant when the list changes meaning i gotta do searches which i am trying to avoid but looks like there is no other choice now :(:(

    edit
    For anyone that is interested this is my solution

            public static int laserSearch(int[] vals)//searches through the laser targets to find its index

            {

                for(int i = 0; i < laserTarget.Count; i++){

                    if(vals[0] == laserTarget[i][0])

                        if(vals[1] == laserTarget[i][1])

                            return i;

                }

                return 0;//should never be reached

            }

  • 11/19/2009 9:06 AM In reply to

    Re: Removing int[] from lists

    If the int arrays in your list always consist of two elements, you could use a struct instead. If those are coordinates, simply use XNA's Point struct, or otherwise write your own with custom Equals() and GetHashCode() implementations.

    This would make the List<>.Remove() method behave as expected and also avoid the forming of collectable garbage due to the array instances you're creating.
    Check out my website and blog for some interesting articles and useful utility classes!
    Nuclex Framework: threaded particles, skinnable GUI, vector fonts, texture atlasses and lots more.
    WiX XNA Installer: Professional-looking MSI installer template for XNA games.
  • 11/21/2009 12:38 PM In reply to

    Re: Removing int[] from lists

    Cygon4:
    If the int arrays in your list always consist of two elements, you could use a struct instead. If those are coordinates, simply use XNA's Point struct, or otherwise write your own with custom Equals() and GetHashCode() implementations.

    This would make the List<>.Remove() method behave as expected and also avoid the forming of collectable garbage due to the array instances you're creating.

    That sounds pretty interesting but i have never worked with structs could you give me a small example?
  • 11/21/2009 6:45 PM In reply to

    Re: Removing int[] from lists

    Cygon is saying instead of this:
    List<int[]> points = new List<int[]>(); 
    points.Add(new int[]{0,0}); 
    points.Remove(somePoint); 

    You should use this:
    List<Point> points = new List<Point>(); 
    points.Add(new Point(0, 0)); 
    points.Remove(somePoint); 

  • 11/21/2009 6:57 PM In reply to

    Re: Removing int[] from lists

    Answer
    Reply Quote
    Right, especially since it allows you to do this:

    List<Point> points = new List<Point>();  
    points.Add(new Point(0, 0));  
    points.Remove(new Point(0, 0));  

    If it's not X and Y coordinates you're storing, you can define your own structure:

    /// <summary> 
    ///   Stores the starting point and the ending point of a segment 
    /// </summary> 
    struct Segment { 
     
      /// <summary>Where the segment begins</summary> 
      public int Begin; 
      /// <summary>Where the segment ends</summary> 
      public int End; 
     
      /// <summary>Initializes a new segment</summary> 
      /// <param name="begin">Where the segment begins</param> 
      /// <param name="end">Where the segment ends</param> 
      public Segment(int begin, int end) { 
        this.Begin = begin; 
        this.End = end; 
      } 
     
      /// <summary>Returns the hash code for this instance</summary> 
      /// <returns> 
      ///   A 32-bit signed integer that is the hash code for this instance 
      /// </returns> 
      public override int GetHashCode() { 
        return 
          this.Begin.GetHashCode() ^ 
          this.End.GetHashCode(); 
      } 
     
      /// <summary> 
      ///   Indicates whether this instance and a specified object are equal 
      /// </summary> 
      /// <param name="otherObject">Another object to compare to</param> 
      /// <returns> 
      ///   True if obj and this instance are the same type and represent 
      ///   the same value; otherwise, false 
      /// </returns> 
      public override bool Equals(object otherObject) { 
        if(!(otherObject is Segment)) { 
          return false
        } 
         
        // Other is a Segment instance, do a member comparison 
        Segment other = (Segment)otherObject; 
        return 
          (this.Begin == other.Begin) && 
          (this.End == other.End); 
      } 
     

    Which allows you to do the same as the Point class, but with your own field names.
    Check out my website and blog for some interesting articles and useful utility classes!
    Nuclex Framework: threaded particles, skinnable GUI, vector fonts, texture atlasses and lots more.
    WiX XNA Installer: Professional-looking MSI installer template for XNA games.
  • 11/22/2009 11:52 AM In reply to

    Re: Removing int[] from lists

    Wow thanks cygon that was great, at first glance it looked too complicated for what i was on about but no that is perfect!
Page 1 of 1 (9 items) Previous Next