# Can't Get A Specific Index From an ArrayList

Hello, I’m trying to get a specific index from my array list.

I have tried multiple things but I always fail to achieve my goal. In my blueprint I calculate eight different vectors and make an array of them. Then I would like to calculate their distances to the player location and get the index of the one array element that has the vector closest to the player…

I have the distance calculated so far but since it is in this for loop I’m struggling to compare all array elements with each other in order to find the one with the smallest distance to the player, also I will need the index of that array element with the shortest distance for later use…

Can someone help my out?

Ok two points

1. You could make a variable set it to the max value that a float can be (call it something like CurMinDistance), do this outside the loop, then compare your distance, to this CurMinDistance, if it’s less (which is why we set it to the max value, so that it is forced to be greater than any value you will get). Then just set the new CurMinDistance value with the distance you compared with. You could also update a index variable in the array to “remember” the new value that you set. Hence when you fall out of hte loop, you instantly have what you need.

If you follow this logic, you will not even need the array, nor an index into it, unless you need all 8 values for somethign later on. When you update CurMinDistance, just update a new vector variable, with the current vector, your getting the distance for.

.

I’m sorry but I’m really struggling to get this done. Would you be so kind and show me how it’s done? I really want to make it with a For Each Loop and at the end I need the index of the one vector that gives the smallest distance. That vector with the smallest distance is going the be the next center point and I need the index to get it from the make array list… You would do me a real favor if you could spare the time… I keep on trying in the mean time…

I think that I know what you want, here is what I did:

I use a function, this bassicly just is vector a - vector b, but it is easier to remember for me.

## Also you need to let closestvector have a high default value and set it to high again if you want to re-use thi logic You actually van add this to the completed part of your for each loop since you have the index of the array that has is the closest, but it depends on what logic it is used for.

Here is one possible solution.

.

This is perfect, thanks a lot! I see now why I couldn’t get it to work, also thanks for the detailed description! The array that feeds the for each loop will have to change after some time but that’ll be easy to achieve. Again thanks for your help!