1 개요[ | ]
- 자바 indexOf()
Java
Copy
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
}
}
Java
Copy
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 같이 보기[ | ]
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.
- 분류 댓글:
- Java (3)
Java 튜토리얼 웹사이트 ― Thesmolt젠킨스 ― John Jeong젠킨스 ― Icesky