利用Collections工具类查找一个字符串在字符串数组里的位置即其角标

package cn.com;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
//要求:查找一个字符串在字符串数组里的位置即其角标
//Collections.sort()方法和Collections.binarySearch()方法;
//注意Collections.binarySearch()是按照二分法查找,所以要先使用Collections.sort()使集合按照自然顺序排序
public class Test3 {
	public static void main(String[] args) {
		String [] strArr=new String[]{"ab","zd","wum","nlk"};
	    List listStr=Arrays.asList(strArr);
	    Collections.sort(listStr);
	    int local=Collections.binarySearch(listStr, "ab");
	    System.out.println("local="+local);//排序后的位置
	}
}

你可能感兴趣的:(J2SE)