Keeping track of some array elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ryushinyama
    New Member
    • Feb 2007
    • 23

    Keeping track of some array elements

    I have an array:

    WeirdObject[] Things ;

    Now lets say I want to keep track of some of those elements of the array that have a property of Things[i].xCoordinate = 5;

    I have been using another array with 12 elements because there can be no more then 12 Things that fit the criteria at one time but there can be as little as 0. I have been holding -1 for what I would call an "Empty" Element.

    So if Things.Length == 77; I could end up with say 3 with .xCoordinate == 5;

    HoldData[0] = 18
    HoldData[1] = 12
    HoldData[2] = 52
    HoldData[3] = -1
    HoldData[4] = -1
    HoldData[5] = -1
    HoldData[6] = -1
    HoldData[7] = -1
    HoldData[8] = -1
    HoldData[9] = -1
    HoldData[10] = -1
    HoldData[11] = -1

    Then use the HoldData Array to manipulate those 3 Things. Maybe even add to and take away from that set. When done with the set, just reset all to -1.


    Now the question...Is there a better way? This has a lot of over head and looping and checking and I just feel that there is a method/concept that I am just not familiar with.

    Thanks in advance
  • Monomachus
    Recognized Expert New Member
    • Apr 2008
    • 127

    #2
    Originally posted by ryushinyama
    I have an array:

    WeirdObject[] Things ;

    Now lets say I want to keep track of some of those elements of the array that have a property of Things[i].xCoordinate = 5;

    I have been using another array with 12 elements because there can be no more then 12 Things that fit the criteria at one time but there can be as little as 0. I have been holding -1 for what I would call an "Empty" Element.

    So if Things.Length == 77; I could end up with say 3 with .xCoordinate == 5;

    HoldData[0] = 18
    HoldData[1] = 12
    HoldData[2] = 52
    HoldData[3] = -1
    HoldData[4] = -1
    HoldData[5] = -1
    HoldData[6] = -1
    HoldData[7] = -1
    HoldData[8] = -1
    HoldData[9] = -1
    HoldData[10] = -1
    HoldData[11] = -1

    Then use the HoldData Array to manipulate those 3 Things. Maybe even add to and take away from that set. When done with the set, just reset all to -1.


    Now the question...Is there a better way? This has a lot of over head and looping and checking and I just feel that there is a method/concept that I am just not familiar with.

    Thanks in advance
    Yep if you're using .NET 3.0 or more please study a little bit LINQ. What I ended up understanding is that you have some kind of array where what you're really interested in is only some specific elements.
    Than you work with those elements. I didn't actually understand what you are doing with HoldData, anyway hope you'll get the point.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WeirdObjects
    {
        static class Program
        {
            public static void Main()
            {
                WeirdObject[] things = InitializeThings();
    
                var thingsX5 = things.Where(el => el.X == 5).ToArray();
    
                foreach (var thingWeird in thingsX5)
                {
                    Console.WriteLine(thingWeird);
                }
    
                Console.ReadKey();
            }
    
            private static WeirdObject[] InitializeThings()
            {
                List<WeirdObject> weirdObjects = new List<WeirdObject>();
    
                Random r = new Random();
    
                for (int i = 0; i < 77; i++)
                {
                    WeirdObject w = new WeirdObject
                                        {
                                            X = r.Next(0, 15),
                                            Y = r.Next(0, 15)
                                        };
                    
                    weirdObjects.Add(w);
                }
    
                return weirdObjects.ToArray();
            }
    
    
        }
    
        internal class WeirdObject
        {
            public int X { get; set; }
            public int Y { get; set; }
    
            public override string ToString()
            {
                return string.Format("X={0}, Y={1}", X, Y);
            }
        }
    }
    And output is

    Code:
    X=5, Y=7
    X=5, Y=6
    X=5, Y=6
    X=5, Y=1
    X=5, Y=5

    Comment

    • ryushinyama
      New Member
      • Feb 2007
      • 23

      #3
      Yes, new to c# and >= 3.0 and more importantly for this project XNA. But the idea of using a where clause on an array sure makes me feel shiny.

      I have been seeing the Linq included in the projects I was working on and just have not gotten around to playing with it yet. I have seen articles in the way of dealing with XML. I see it has a much wider scope then just that.

      I will be giving Linq some real attention soon. Thanks for your help!

      PS. The HoldData array was being used like the thingsX5 accept the HoldData array is a list of pointers instead of a copy of the elements.

      I pull in all items that meet a criteria like being the color red. Move them around or change colors or whatever needed to be done with them and then clear out the pointers when done with them. That way I can loop through the 7x larger array many less times while dealing with that set.

      I am assuming here that one can use the Linq extensions to push the elements from thingsx5[] back into things[].

      Comment

      Working...