开发者

C# Select list items at the same index as another list

开发者 https://www.devze.com 2023-04-09 22:30 出处:网络
A little stuck once again. I have been trying to find a way to code the following (all of my attempts failed unfortunately...)

A little stuck once again.

I have been trying to find a way to code the following (all of my attempts failed unfortunately...)

I have two arrays/lists whatever you want to call them. Both have different items in them, although they will always have the same number of items. For the sake of my question, these lists have 3 items each.

How would one go about coding it so that if I take item 2 from list 1, item 2 from list 2 would also be transported.

So.. effectively.. If I was to move item 2 from list 1 to list 3, I would like item 2 from list 2 to go to list 4.

If that makes sense...

Do not have any code to show off as I have not found a way to code this yet.. Any kind souls that would be able to educate me in this matter?

Thank you.

EDIT

The actual thing I am trying to do is this:

I have a list of real-time generated hashes, a list of locations for files those hashes came from, and a list of healthy hashes. I am comparing both hash lists to find differences, then I am listing the differences. (all of which I have already coded) THe last thing i want to do is by using the listed hashes that do not match the healthy ones, return their directory (which is already in the list mentioned above)

string path = Application.ExecutablePath; // <------- Get full file path from this EXE
            string root = Path.GetDirectoryName(path); // <------- Get just the directory from above file path
            IEnumerable<string> list0 = File.ReadLines(root + "/healthy.txt");
            List<string> list1 = new List<string>();
            IEnumerable<string> list2 = Directory.EnumerateFiles(root, "*.lol", SearchOption.AllDirectories);
            List<string> list3 = new List<string>();
            foreach (string forone in list0)
            {
                list1.Add(forone.ToString());
            }
            fore开发者_Go百科ach (string obj in list2)
            {
                listBox4.Items.Add(obj.ToString());
                list3.Add(GetMD5HashFromFile(obj.ToString()));
            }
            foreach (string obj2 in list3)
            {
                listBox2.Items.Add(obj2.ToString());
            }
            foreach (string hea in list1)
            {
                listBox1.Items.Add(hea.ToString());
            }
            IList<string> difference = list3.Except(list1).ToList();
            foreach (string something in difference)
            {
                listBox3.Items.Add(something.ToString());
            }

Please note that listboxes are only used for troubleshooting and so I can see what is in the arrays.


Create a wrapper class that holds both items and make a single list of that. You can easily convert your combined list into a list of the objects you are interested in if you need to:

List<Item1> item1s = combinedList.ConvertAll(pair => pair.Item1);


Generic:

public class SynchronizedListsWrapper<T>
{
    private readonly List<T> _first;
    private readonly List<T> _second;

    public SynchronizedListsWrapper(List<T> first, List<T> second)
    {
        _first = first;
        _second = second;
    }

    public void Add(T item)
    {
        first.Add(item);
        second.Add(item);
    }

    public void Remove(T item)
    {
        first.Remove(item);
        second.Remove(item);
    }
}

Or without generics:

public class SynchronizedListsWrapper
{
    private readonly ArrayList _first;
    private readonly ArrayList _second;

    public SynchronizedListsWrapper(ArrayList first, ArrayList second)
    {
        _first = first;
        _second = second;
    }

    public void Add(object item)
    {
        first.Add(item);
        second.Add(item);
    }

    public void Remove(object item)
    {
        first.Remove(item);
        second.Remove(item);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号