数组

一】数组的三种定义方法:
  1》元素类型[] 数组名 = new 元素类型[元素个数或数组长度];
  2》元素类型[] 数组名 = new 元素类型[元素1,元素2, 元素3...];
  3》元素类型[] 数组名 = {元素1,元素2, 元素3...};
  
 二】数组的简单输出方法:
   通过Arrays类中的toString方法:
    ·方法原型: static String toString(type[] a);//其中type表示基本数据类型(int, double, long...)
    
 三】数组复制的两种方法
 
  1》通过System类的arraycopy方法:
   ·方法原型: static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
  2》通过Arrays类中的copyOf方法         
   ·方法原型: static type[] copyOf(type[] src, int newLength);//截取或用 0 填充(如有必要),以使副本具有指定的长度newLength。
      ==>此方法可以完成数组的扩展,示例:
       char[] chs = {'北', '京'};
       char[] temp = Arrays.copyOf(chs, chs.length+2);
       chs = temp;//丢弃原数组,引用新数组
       chs[2] = '达';
       chs[3] = '内';
       System.out.println(chs);//北京达内。
 四】数组的排序
  通过Arrays类中的sort方法:
   ·方法原型:
       static void sort(type[] arr);
       static void sort(type[] arr, int fromIndex, int endIndex);//不包括 endIndex下标的元素
 
 五】数组的查找
  通过Arrays类中的binarySearch方法。
   ·方法原型:
       static int binarySearch(type[] arr, type key);
       
       返回值: 存在,返回下标;  不存在,返回(【可插入点相反数】-1).
       注意:首先必须使用sort方法对数组进行排序。

 

原文地址:https://www.cnblogs.com/SkyGood/p/3941907.html