Arrays工具类常用方法总结一(重要介绍Arrays.binarySearch()方法)

1、Arrays与Array的区别

  Array(数组类):是java中的最近本的一个存储结构,其中的数据类型要一致。
  Arrays(静态类):是专门用来操作Array的,提供搜索、排序、复制等静态方法。

2、对 ArrayList 中的汉字(例如:姓名等)按照首字母排序

class SortByName implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            Student s1 = (Student) o1;
            Student s2 = (Student) o2;
	//在这里我先是转化为拼音再进行的排序直接以下方法排序可能会出现错误数据
            return  s1.getName().compareTo(s2.getName());
        }
    }

   //根据姓名进行排序
   Collections.sort(list, new SortByName());

3、打印数组

 String[] stringArray = { "a", "b", "c", "d", "e" };
   Arrays.toString(stringArray );

4、将数组转换为list

  String[] stringArray = { "a", "b", "c", "d", "e" };
  Arrays.asList(stringArray);

5、找到数组中某一个元素的索引

   String[] arrays = new String[]{"a","b","c","d","e","fff","g","h","i","j",};
        int positon = Arrays.binarySearch(arrays, "fff");  
        System.out.println("position is:"+positon);

        这里有一个坑需要注意:
       public static void main(String[] args) {
	 String[] arrays = new String[]{"3sefythe", "6rtww"," 4yvnsft", 
			 "3i3a1", "3gadxz", "2x11jc7", "5tvn4", 
			 "1lqylfsq", "17xdrb", "1fheo", "5h04mnj6", 
			 "4t7r1m", "12yw8x", "4r812yt", "j1wxgj11", 
			 "1edntr7g8", "3uexgtsfz", "4dtvsb"};
                 int positon = Arrays.binarySearch(arrays, "1edntr7g8"); 
	 System.out.println("position is:"+positon);
  

像上面这种数组元素中既有数字也有字母的时候排序是错误的,结果会出现负数。

我在网上搜了一下,Arrays.binarySearch这个方法是通过二分法实现的比较找索引
而二分法查找方法法:必须是有序数据,可以是数字,字母等下面是 binarySearch方法的实现:

 public static int binarySearch(char[] array, int startIndex, int endIndex, char value) {  
            checkBinarySearchBounds(startIndex, endIndex, array.length);
      
            int lo = startIndex;  
            int hi = endIndex - 1;  
      
            while (lo <= hi) {  
                int mid = (lo + hi) >>> 1;//无符号右移  
                char midVal = array[mid];  
      
                if (midVal < value) {  
                    lo = mid + 1;  
                } else if (midVal > value) {  
                    hi = mid - 1;  
                } else {  
                    return mid;  // value found  
                }  
            }  
            return ~lo;  // value not present  
        }

像上面那样的无序字符串取索引,可以通过遍历的方法来取

      String str = "1edntr7g8";
	 int positon = -1;
	 String[] arrays = new String[]{"3sefythe", "6rtww"," 4yvnsft", 
			 "3i3a1", "3gadxz", "2x11jc7", "5tvn4", 
			 "1lqylfsq", "17xdrb", "1fheo", "5h04mnj6", 
			 "4t7r1m", "12yw8x", "4r812yt", "j1wxgj11", 
			 "1edntr7g8", "3uexgtsfz", "4dtvsb"};
	 for (int i = 0; i < arrays.length; i++) {
		if(arrays[i].equals(str)) {
			positon = i;
		}
	 }
	 System.out.println("position is:"+positon);

 

 

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值