In this method i want first to check the distances.
For example in the List pairList in index 0,1,2 all the distances are the same all of them are 10.
So i want to add them to a List samedistances.
Then to continue loop over the pairList until the next part where distances will be the same and add them also to the samedistances list.
So in the end the samedistances list should be look something like:
10
10
10
20
20
44
44
44
44
50
50
But the way it is now it’s adding first only two distances of 10 but there are three.
Then it’s adding one distance of 14.43455 but there is only one of that distance.
And the first distance is 0 so not need to compare it at all.
And last in the end i’m getting exception ArgumentOutOfRangeException: Argument is out of range since i’m doing i+1
private void MoveInPath()
{
List<float> samedistances = new List<float>();
for (int i = 0; i < pairList.Count; i++)
{
if (pairList[i].Distance == pairList[i+1].Distance)
{
samedistances.Add(pairList[i].Distance);
}
}
}
This is working but i will explain why i tried to use the way i did it before:
for (int i = 0; i < pairList.Count; i++)
{
if (pairList[i].Distance == pairList[i+1].Distance)
{
samedistances.Add(pairList[i].Distance);
}
}
The reason is that i want to get random distance item out of same distances.
For example if there three 10 distances then pic random one of them.
The distances are the same three same 10 distances but each one have another position.
pairList*.Position* So what i want is each group of same distances to pick one random. So for example there three distances of 10: Distance 10 - Position 1,1,1 Distance 10 - Position 1,23,45 Distance 10 - Position 1,2,2 So i want to pick a random distance and then use the position of this distance.
Well, you’re not checking your List’s bounds while you interate/index (it).
You’re comparing index ‘i’ with ‘i+1’ (while the loop is < …Count), which means the last entry will always be out of bounds, at least.
To expand on your design goal for wanting to compare as many that are the same, I’d say you could do an inner loop , maybe like:
List<List<int>> samelist = new List<List<int>>();
List<int> num = new List<int>();
num.Add(5);
num.Add(5);
num.Add(7);
num.Add(7);
num.Add(7);
num.Add(7);
num.Add(10);
num.Add(20);
num.Add(74);
num.Add(74);
num.Add(2);
num.Add(98);
num.Add(98);
num.Add(98);
num.Add(98);
num.Add(98);
int lastmatch = -1;
int b = num.Count - 1;
for(int i = 0; i < num.Count; ++i){
if(i < b && num[i] == num[i+1]) {
samelist.Add(new List<int>());
samelist[samelist.Count-1].Add(num[i]);
samelist[samelist.Count-1].Add(num[i]);
int j = i+2;
while(j <= b) {
if(num[j] == num[i]){
samelist[samelist.Count-1].Add(num[i]);
++i;
++j;
}
else break;
}
}
}
for(int i = 0; i < samelist.Count; ++i){
Console.WriteLine("Same List index " + i);
Console.WriteLine("Value = " + samelist[i][0]);
Console.WriteLine("Count = " + samelist[i].Count);
}
(written and moved from rextester - spacing’s a little whacky).
I had originally mentioned that (though didn’t put it in my own example). However, I got the impression his question was asking for a little more than a snippet like that…
Okay, just reread the question. My solution is only the first step, although it will fix the error.
This is actually a surprisingly common problem I encounter in my day job. The trick is to look for the differences, not the equality. Its basically an edge detection problem.