only keep every 10th entry in an array?
hey! i have this huge list of arrays:
in my
public Vector3[] paths;
but i wanna kick out some of them. so that i keep Element 0 , 10 , 20 and so on. how can i do this?
Answer by ESchrauwen · Nov 14, 2016 at 02:24 PM
Iterate over your list with a for-loop. Use the modulus operator.
With "i" as your current index, "i % 10" will be 0 for each 10th item.
optimizedpoints = new Vector3[points.Length%10];
int j = 0;
for (int i = 0; i < points.Length; i%=10) {
optimizedpoints[j] = points[i];
j++;
}
seams to be wrong somehow? do you know why?
newPoints = new List<Vector3>();
for (int i = 0; i < points.Length; ++i) {
if( i % 10 == 0 )
optimizedpoints.Add( points[i] );
}
optimizedPoints = newPoints.ToArray();
Answer by skillbow · Nov 14, 2016 at 01:42 PM
Maybe create a new paths array and in a for loop, copy every 10th element from the initial paths array. Something like:
Vector3[] newPaths = new Vector3[paths.Length/10];
int j = 0;
for (int i = 0; i < paths.Length; i+=10) {
newpaths[j] = paths[i];
j++;
}
You can then clear paths and copy newPaths into it.
thanks! thats almost perfect. i am just always using the last point. kinda bad since its meant for a coordinate system :D do you have an idea for that?
No problem. When you say the last point, do you mean that you use the last point in the array (paths[paths.length])?
Your answer
Follow this Question
Related Questions
Find the closest Enemy that has a tag added in a list? 1 Answer
Array problem, I'm novice 1 Answer
Getting the element that has the same element number on another list ? 2 Answers
I have trouble understanding arrays and enums. When and how? 0 Answers
Getting an error when using an Array of String Arrays 0 Answers