Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find an Index in an array using a value that exists in another array and THAT array?

Tags:

arrays

c#

I have a c# class that looks like this:

   public class MemberData
{
    public int meme_ck;
    public string meme_name;
    public bool meme_active;

    public MemberData(int ck2, string name2, bool active2)
    {
        meme_ck = ck2;
        meme_name = name2;
        meme_active = active2;
    }
}

I have made two arrays out of that class:

    private MemberData[] memarray1 = new MemberData[10000];
    private MemberData[] memarray2 = new Memberdata[10000];

Over the course of my application I do a bunch of stuff with these two arrays and values change, etc. Member's name or active status may change which results in the ararys becoming different.

Eventually I need to compare them in order to do things to the other one based on what results are kicked out in the first one.

For example, member is de-activated in the first array based on something application does, I need to update array 2 to de-activate that same member.

I am trying to use some database design philosphy with the int CK (contrived-key) to be able to rapidly look up the entry in the other array based on the CK.

Since I can't figure it out I've had to resort to using nested for loops like this, which sucks:

        foreach (Memberdata md in memarray1)
    {
        foreach (Memberdatamd2 in memarray2)
        {
            if (md.ck = md2.ck)
            {
                //de-activate member
            }
        }
    }

Is there a better way to do this? I just want to find the index in the second array based on CK when I have the CK value from the first array.

Any other tips or advice you have about structure would be appreciated as well. Should I be using something other than arrays? How would I accomplish this same thing with Lists?

Thanks!

like image 709
john williams Avatar asked Nov 17 '25 19:11

john williams


1 Answers

Should I be using something other than arrays?

Yes. Don't use arrays; they are seldom the right data structure to use.

How would I accomplish this same thing with Lists?

Lists are only marginally better. They don't support an efficient lookup-by-key operation which is what you need.

It sounds like what you want is instead of two arrays, two Dictionary<int, MemberData> where the key is the ck.

like image 51
Eric Lippert Avatar answered Nov 19 '25 09:11

Eric Lippert