자바 indexOf()

1 개요[ | ]

자바 indexOf()
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
    }
}
public class MyClass {
    public static void main(String args[]) {
        String fruits[] = {"Banana", "Orange", "Apple", "Orange", "Apple"};
        int index = -1;
        for(int i=0; i<fruits.length; i++) {
            if( fruits[i].equals("Apple") ) { index=i; break; }
        }
        System.out.println( index ); // 2
        index = -1;
        for(int i=0; i<fruits.length; i++) {
            if( fruits[i].equals("Pineapple") ) { index=i; break; }
        }
        System.out.println( index ); // -1
    }
}

2 같이 보기[ | ]

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