0

I have create an array of integers.

Dim EnemyLevel() as Integer = {1,2,2,3}

Let's say I wanted to get the position of every item that included '2', that'd be EnemyLevel(1) and EnemyLevel(2), what's the function to do that?

I'm supossed to use Array.IndexOf(EnemyLevel(),2) but how do I store the results?

1 Answer 1

1

You run the array in a For Loop. And check if any of the indices contain '2'

Array1 = Array(1,2,2,4)    
For i=0 to UBound(Array1)    
   If Array1(i) = 2 Then    
      msgbox i    
   End If    
Next
Sign up to request clarification or add additional context in comments.

4 Comments

This was so obvious I'm mad at myself. Thanks a lot for your answer.
This doesn't really answer the question on two counts. Firstly, it doesn't use Array.IndexOf, which the question states is required. Maybe that's not actually required though, as a For loop would be the usual way. It can be done with a Do or While loop and calling Array.IndexOf though. Secondly, it doesn't actually say anything about storing the results. It simply displays them. Maybe how to store them became obvious once it was seen how to get them or, again, maybe storing them wasn't actually required.
You can have multiple solutions to a problem and all of them can be right. As long as the logic is clear. Rest of it should be pretty straight.
I THOUGHT I had to use Array.IndexOf, but the solution provided answered my question about looking for the results just fine. As for storing them, yeah, that was obvious afterwards. Thanks for noticing anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.