473,411 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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
Apr 27 '10 #1
2 1780
Monomachus
127 Expert 100+
@ryushinyama
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.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5.  
  6. namespace WeirdObjects
  7. {
  8.     static class Program
  9.     {
  10.         public static void Main()
  11.         {
  12.             WeirdObject[] things = InitializeThings();
  13.  
  14.             var thingsX5 = things.Where(el => el.X == 5).ToArray();
  15.  
  16.             foreach (var thingWeird in thingsX5)
  17.             {
  18.                 Console.WriteLine(thingWeird);
  19.             }
  20.  
  21.             Console.ReadKey();
  22.         }
  23.  
  24.         private static WeirdObject[] InitializeThings()
  25.         {
  26.             List<WeirdObject> weirdObjects = new List<WeirdObject>();
  27.  
  28.             Random r = new Random();
  29.  
  30.             for (int i = 0; i < 77; i++)
  31.             {
  32.                 WeirdObject w = new WeirdObject
  33.                                     {
  34.                                         X = r.Next(0, 15),
  35.                                         Y = r.Next(0, 15)
  36.                                     };
  37.  
  38.                 weirdObjects.Add(w);
  39.             }
  40.  
  41.             return weirdObjects.ToArray();
  42.         }
  43.  
  44.  
  45.     }
  46.  
  47.     internal class WeirdObject
  48.     {
  49.         public int X { get; set; }
  50.         public int Y { get; set; }
  51.  
  52.         public override string ToString()
  53.         {
  54.             return string.Format("X={0}, Y={1}", X, Y);
  55.         }
  56.     }
  57. }
  58.  
And output is

Expand|Select|Wrap|Line Numbers
  1. X=5, Y=7
  2. X=5, Y=6
  3. X=5, Y=6
  4. X=5, Y=1
  5. X=5, Y=5
  6.  
Apr 27 '10 #2
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[].
Apr 30 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing...
4
by: Deniz Bahar | last post by:
Hello all, Often times programs in C have arrays used as buffers and shared among different sections of code. The need arises to have position indicators to point to different parts of an array...
1
by: Joshua Russell | last post by:
I'm writing some software that opens multiple instances of a single class in new threads. I need a way of keeping track of all these class instances in some kind of collection. I'm basicaly writing...
9
by: Alfred Taylor | last post by:
I'm testing the waters of n-tier development and I ran into a scenario that I'm not sure what the best solution would be. I have a Company object which contains a collection of contacts retrieved...
7
by: murrayatuptowngallery | last post by:
After a few searches & inquiries here, I have a method of generating an html table populated with results of equations that works and wasn't too far up a learning curve for me, thanks to a helpful...
19
by: pinkfloydhomer | last post by:
Please read the example below. I'm sorry I couldn't make it smaller, but it is pretty simple. When client code is calling newThingy(), it is similar to malloc: the client gets a pointer to a...
2
by: metaperl | last post by:
I'm actually taking Microsoft's 2779 and just finished a lab where we kept track of our changes to the database. However, I'm not happy with the scripts interface because it does not tell me the...
5
by: jason735 | last post by:
Hi, I've got the following problem. I have to sort x*y elements which are in one file. I can use only an array for x elements and floor tmp files which can be read only forward. Thanks for...
7
by: arnuld | last post by:
this programme gives unusual output. i am not able to find out where the semantic bug lies: /* C++ Primer - 4/e * * an example from section section 7.2.4, page 241 * STATEMENT * write a...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.