Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

c# - How to compare two lists to determine if they have required values on the same index


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can have a list of Coordinates like:

public class Coordinate 
{
    public int X {get; set;}
    public int Y {get; set;}
}

and your List as List<Coordinate>() this way you can do it easily like:

bool exists = list.Any(l => l.X == x && x.Y == y);

However if you insist on having 2 separate lists, you can do it with a simple loop

int i = 0;
for(i=0;i<xList.Length;i++)
{
    if(xList[i] == x && yList[i] == y) break; 
}

if(i > xList.Length)
  // not found
else
{
    //i is the index
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...