简体   繁体   中英

How to get an item from a list by its property, and then use it's other property?

class Node
{
    int number;
    Vector2 position;
    public Node(int number, Vector2 position)
    {
        this.number = number;
        this.position = position;
    }
}

List<Node>nodes = new List<Node>();

for (int i = 0; i < nodes.Count; i++) //basically a foreach
{
    // Here i would like to find each node from the list, in the order of their numbers, 
    // and check their vectors
}

So, as the code pretty much tells, i am wondering how i can

  1. find a specific node from the list, specifically one with the attribute "numbers" being i (Eg going through all of them in the order of their "number" attribute).
  2. check its other attribute

Have tried:

nodes.Find(Node => Node.number == i);

Node test = nodes[i];
place = test.position

they cant apparently access node.number / node.position due to its protection level.

Also the second one has the problem that the nodes have to be sorted first.

Also looked at this question

but [] solution is in the "Tried" caterology, foreach solution doesn't seem to work for custom classes.

I'm a coding newbie (Like 60 hours), so don't

  1. Explain in a insanely hard way.
  2. Say i am dumb for not knowing a this basic thing.

Thanks!

I would add properties for Number and Position , making them available to outside users (currently their access modifier is private ):

class Node
{
    public Node(int number, Vector2 position)
    {
        this.Number = number;
        this.Position = position;
    }

    public int Number { get; private set; }
    public Vector2 Position { get; private set; }
}

Now your original attempt should work:

nodes.Find(node => node.Number == i);

However, it sounds like sorting the List<Node> and then accessing by index would be faster. You would be sorting the list once and directly indexing the list vs looking through the list on each iteration for the item you want.

List<Node> SortNodes(List<Node> nodes)
{
   List<Node> sortedNodes = new List<Node>();
   int length = nodes.Count; // The length gets less and less every time, but we still need all the numbers!
   int a = 0; // The current number we are looking for


   while (a < length)
   {
      for (int i = 0; i < nodes.Count; i++)
      {
         // if the node's number is the number we are looking for, 
         if (nodes[i].number == a)
         {
            sortedNodes.Add(list[i]); // add it to the list
            nodes.RemoveAt(i); // and remove it so we don't have to search it again.
            a++; // go to the next number
            break; // break the loop
         }
      }
   }

   return sortedNodes;
}

This is a simple sort function. You need to make the number property public first. It will return a list full of nodes in the order you want to. Also: The searching goes faster as more nodes are added to the sorted nodes list.

Make sure you that all nodes have a different number! Otherwise it would get stuck in an infinite loop!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM