함수 index()

(Index에서 넘어옴)
index()
array_search()

1 Java[ | ]

import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
        String fruits[] = {"Banana", "Orange", "Apple", "Orange", "Apple"};
        System.out.println( Arrays.asList(fruits).indexOf("Apple") ); // 2
        System.out.println( Arrays.asList(fruits).indexOf("Pineapple") ); // -1
    }
}

2 PHP[ | ]

$fruits = ['Banana', 'Orange', 'Apple', 'Orange', 'Mango', 'Orange'];
var_dump( array_search('Orange', $fruits) );
# int(1)
var_dump( array_search('Pineapple', $fruits) );
# bool(false)

3 Python[ | ]

fruits = ['Banana', 'Orange', 'Apple', 'Orange', 'Mango', 'Orange']
print( fruits.index('Orange') )
# 1
print( fruits.index('Orange', 4) )
# 5
print( fruits.index('Orange', 2, 6) )
# 3
fruits = ['Banana', 'Orange', 'Apple', 'Mango']
print( fruits.index('Pineapple') )
# ValueError: 'Pineapple' is not in list

4 Ruby[ | ]

a = [ "a", "b", "c" ]
p a.index("b")
# 1
p a.index { |x| x == "b" }
# 1
p a.index("z")
# nil

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}