Arrays中一些方法的使用

static intbinarySearch(int[] a, int key)
          使用二分搜索法来搜索指定的 int 型数组,以获得指定的值。

binarySearch

public static int binarySearch(int[] a,
                               int key)
使用二分搜索法来搜索指定的 int 型数组,以获得指定的值。必须在进行此调用之前对数组进行排序(通过 sort(int[]) 方法)。如果没有对数组进行排序,则结果是不确定的。如果数组包含多个带有指定值的元素,则无法保证找到的是哪一个。

例:import java.util.Arrays;

public class Test {

 public static void main(String[] args) {

  int[] a = { 1, 2, 34, 5, 6, 534, 6452, 432, 54 };
  Arrays.sort(a);// 必须先对a进行排序
  int b = Arrays.binarySearch(a, 432);
  System.out.println(b);
 }

}
输出结果:6

 

 

static intbinarySearch(char[] a, char key)
          使用二分搜索法来搜索指定的 char 型数组,以获得指定的值。

例:import java.util.Arrays;

public class Test {

 public static void main(String[] args) {

  char[] a = { 'c', 'd', 'm', 'a', 'b', 'l' };
  Arrays.sort(a);// 对a进行排序
  int b = Arrays.binarySearch(a, 'a');
  System.out.println(Arrays.toString(a));
  System.out.println(b);
 }

}

输出结果:[a, b, c, d, l, m]
               0

 

 

static intbinarySearch(byte[] a, byte key)
          使用二分搜索法来搜索指定的 byte 型数组,以获得指定的值。
static intbinarySearch(byte[] a, int fromIndex, int toIndex, byte key)
          使用二分搜索法来搜索指定的 byte 型数组的范围,以获得指定的值。
static intbinarySearch(char[] a, char key)
          使用二分搜索法来搜索指定的 char 型数组,以获得指定的值。
static intbinarySearch(char[] a, int fromIndex, int toIndex, char key)
          使用二分搜索法来搜索指定的 char 型数组的范围,以获得指定的值。
static intbinarySearch(double[] a, double key)
          使用二分搜索法来搜索指定的 double 型数组,以获得指定的值。
static intbinarySearch(double[] a, int fromIndex, int toIndex, double key)
          使用二分搜索法来搜索指定的 double 型数组的范围,以获得指定的值。
static intbinarySearch(float[] a, float key)
          使用二分搜索法来搜索指定的 float 型数组,以获得指定的值。
static intbinarySearch(float[] a, int fromIndex, int toIndex, float key)
          使用二分搜索法来搜索指定的 float 型数组的范围,以获得指定的值。
static intbinarySearch(int[] a, int key)
          使用二分搜索法来搜索指定的 int 型数组,以获得指定的值。
static intbinarySearch(int[] a, int fromIndex, int toIndex, int key)
          使用二分搜索法来搜索指定的 int 型数组的范围,以获得指定的值。
static intbinarySearch(long[] a, int fromIndex, int toIndex, long key)
          使用二分搜索法来搜索指定的 long 型数组的范围,以获得指定的值。
static intbinarySearch(long[] a, long key)
          使用二分搜索法来搜索指定的 long 型数组,以获得指定的值。
static intbinarySearch(Object[] a, int fromIndex, int toIndex, Object key)
          使用二分搜索法来搜索指定数组的范围,以获得指定对象。
static intbinarySearch(Object[] a, Object key)
          使用二分搜索法来搜索指定数组,以获得指定对象。
static intbinarySearch(short[] a, int fromIndex, int toIndex, short key)
          使用二分搜索法来搜索指定的 short 型数组的范围,以获得指定的值。
static intbinarySearch(short[] a, short key)
          使用二分搜索法来搜索指定的 short 型数组,以获得指定的值。
static
<T> int
binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)
          使用二分搜索法来搜索指定数组的范围,以获得指定对象。
static
<T> int
binarySearch(T[] a, T key, Comparator<? super T> c)
          使用二分搜索法来搜索指定数组,以获得指定对象。

 

 

 

static int[]copyOf(int[] original, int newLength)
          复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。

copyOf

public static int[] copyOf(int[] original,
                           int newLength)
复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。对于在原数组和副本中都有效的所有索引,这两个数组将包含相同的值。对于在副本中有效而在原数组无效的所有索引,副本将包含 0。当且仅当指定长度大于原数组的长度时,这些索引存在。

 

参数:
original - 要复制的数组
newLength - 要返回的副本的长度

例:import java.util.Arrays;

public class Test {

 public static void main(String[] args) {

  int[] a = { 1, 2, 3, 4, 5 };
  int[] b = Arrays.copyOf(a, 7);
  b[5] = 6;
  b[6] = 7;
  System.out.println(b.length);
  System.out.println(Arrays.toString(b));
 }

}
输出结果:7
              [1, 2, 3, 4, 5, 6, 7]

 

 

 

 

static boolean[]copyOf(boolean[] original, int newLength)
          复制指定的数组,截取或用 false 填充(如有必要),以使副本具有指定的长度。
static byte[]copyOf(byte[] original, int newLength)
          复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。
static char[]copyOf(char[] original, int newLength)
          复制指定的数组,截取或用 null 字符填充(如有必要),以使副本具有指定的长度。
static double[]copyOf(double[] original, int newLength)
          复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。
static float[]copyOf(float[] original, int newLength)
          复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。
static int[]copyOf(int[] original, int newLength)
          复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。
static long[]copyOf(long[] original, int newLength)
          复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。
static short[]copyOf(short[] original, int newLength)
          复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。
static
<T> T[]
copyOf(T[] original, int newLength)
          复制指定的数组,截取或用 null 填充(如有必要),以使副本具有指定的长度。
static
<T,U> T[]
copyOf(U[] original, int newLength, Class<? extends T[]> newType)
          复制指定的数组,截取或用 null 填充(如有必要),以使副本具有指定的长度。

 

 

static int[]copyOfRange(int[] original, int from, int to)
          将指定数组的指定范围复制到一个新数组。

public static int[] copyOfRange(int[] original,
                                int from,
                                int to)
将指定数组的指定范围复制到一个新数组。该范围的初始索引 ( from) 必须位于 0 和 original.length(包括)之间。 original[from] 处的值放入副本的初始元素中(除非 from == original.lengthfrom == to)。原数组中后续元素的值放入副本的后续元素。该范围的最后索引 ( to)(必须大于等于 from)可以大于 original.length,在这种情况下, 0 被放入索引大于等于 original.length - from 的副本的所有元素中。返回数组的长度为 to - from

 

参数:
original - 将要从其复制一个范围的数组
from - 要复制的范围的初始索引(包括)
to - 要复制的范围的最后索引(不包括)。(此索引可以位于数组范围之外)。
例:import java.util.Arrays;
public class Test {
 public static void main(String[] args) {
  int[] a = { 1, 2, 3, 4, 5 };
  int[] b = Arrays.copyOfRange(a, 1, 5);
  System.out.println(Arrays.toString(b));
 }
}
输出结果:[2, 3, 4, 5]

 

 

 

static booleanequals(int[] a, int[] a2)
          如果两个指定的 int 型数组彼此相等,则返回 true

例:import java.util.Arrays;

public class Test {

 public static void main(String[] args) {

  int[] a = { 1, 2, 3, 4, 5 };
  int[] b = { 2, 3, 5, 4, 1 };
  int[] c = { 1, 2, 3, 4, 5 };

  System.out.println(Arrays.equals(a, b));
  System.out.println(Arrays.equals(a, c));
 }

}
输出结果:false

               ture

static booleanequals(boolean[] a, boolean[] a2)
          如果两个指定的 boolean 型数组彼此相等,则返回 true
static booleanequals(byte[] a, byte[] a2)
          如果两个指定的 byte 型数组彼此相等,则返回 true
static booleanequals(char[] a, char[] a2)
          如果两个指定的 char 型数组彼此相等,则返回 true
static booleanequals(double[] a, double[] a2)
          如果两个指定的 double 型数组彼此相等,则返回 true
static booleanequals(float[] a, float[] a2)
          如果两个指定的 float 型数组彼此相等,则返回 true
static booleanequals(int[] a, int[] a2)
          如果两个指定的 int 型数组彼此相等,则返回 true
static booleanequals(long[] a, long[] a2)
          如果两个指定的 long 型数组彼此相等,则返回 true
static booleanequals(Object[] a, Object[] a2)
          如果两个指定的 Objects 数组彼此相等,则返回 true
static booleanequals(short[] a, short[] a2)
          如果两个指定的 short 型数组彼此相等,则返回 true

 

 

 

 

static voidfill(int[] a, int val)
          将指定的 int 值分配给指定 int 型数组的每个元素。

例:import java.util.Arrays;

public class Test {

 public static void main(String[] args) {

  int[] a = { 1, 2, 3, 4, 5 };
  Arrays.fill(a, 9);

  System.out.println(Arrays.toString(a));

 }

}
输出结果:[9, 9, 9, 9, 9]

static voidfill(boolean[] a, boolean val)
          将指定的 boolean 值分配给指定 boolean 型数组的每个元素。
static voidfill(boolean[] a, int fromIndex, int toIndex, boolean val)
          将指定的 boolean 值分配给指定 boolean 型数组指定范围中的每个元素。
static voidfill(byte[] a, byte val)
          将指定的 byte 值分配给指定 byte 节型数组的每个元素。
static voidfill(byte[] a, int fromIndex, int toIndex, byte val)
          将指定的 byte 值分配给指定 byte 型数组指定范围中的每个元素。
static voidfill(char[] a, char val)
          将指定的 char 值分配给指定 char 型数组的每个元素。
static voidfill(char[] a, int fromIndex, int toIndex, char val)
          将指定的 char 值分配给指定 char 型数组指定范围中的每个元素。
static voidfill(double[] a, double val)
          将指定的 double 值分配给指定 double 型数组的每个元素。
static voidfill(double[] a, int fromIndex, int toIndex, double val)
          将指定的 double 值分配给指定 double 型数组指定范围中的每个元素。
static voidfill(float[] a, float val)
          将指定的 float 值分配给指定 float 型数组的每个元素。
static voidfill(float[] a, int fromIndex, int toIndex, float val)
          将指定的 float 值分配给指定 float 型数组指定范围中的每个元素。
static voidfill(int[] a, int val)
          将指定的 int 值分配给指定 int 型数组的每个元素。
static voidfill(int[] a, int fromIndex, int toIndex, int val)
          将指定的 int 值分配给指定 int 型数组指定范围中的每个元素。
static voidfill(long[] a, int fromIndex, int toIndex, long val)
          将指定的 long 值分配给指定 long 型数组指定范围中的每个元素。
static voidfill(long[] a, long val)
          将指定的 long 值分配给指定 long 型数组的每个元素。
static voidfill(Object[] a, int fromIndex, int toIndex, Object val)
          将指定的 Object 引用分配给指定 Object 数组指定范围中的每个元素。
static voidfill(Object[] a, Object val)
          将指定的 Object 引用分配给指定 Object 数组的每个元素。
static voidfill(short[] a, int fromIndex, int toIndex, short val)
          将指定的 short 值分配给指定 short 型数组指定范围中的每个元素。
static voidfill(short[] a, short val)
          将指定的 short 值分配给指定 short 型数组的每个元素。

 

 

 

 

static inthashCode(int[] a)
          基于指定数组的内容返回哈希码。

例:import java.util.Arrays;

public class Test {

 public static void main(String[] args) {

  int[] a = { 1, 2, 3, 4, 5 };
  char[] aa={'a','b','c','d'};
  int b = Arrays.hashCode(a);
  int c=Arrays.hashCode(aa);

  System.out.println(b);
  System.out.println(c);
 }

}
输出结果:29615266
              3910595

static inthashCode(boolean[] a)
          基于指定数组的内容返回哈希码。
static inthashCode(byte[] a)
          基于指定数组的内容返回哈希码。
static inthashCode(char[] a)
          基于指定数组的内容返回哈希码。
static inthashCode(double[] a)
          基于指定数组的内容返回哈希码。
static inthashCode(float[] a)
          基于指定数组的内容返回哈希码。
static inthashCode(int[] a)
          基于指定数组的内容返回哈希码。
static inthashCode(long[] a)
          基于指定数组的内容返回哈希码。
static inthashCode(Object[] a)
          基于指定数组的内容返回哈希码。
static inthashCode(short[] a)
          基于指定数组的内容返回哈希码。

 

 

 

static voidsort(byte[] a)
          对指定的 byte 型数组按数字升序进行排序。
static voidsort(byte[] a, int fromIndex, int toIndex)
          对指定 byte 型数组的指定范围按数字升序进行排序。
static voidsort(char[] a)
          对指定的 char 型数组按数字升序进行排序。
static voidsort(char[] a, int fromIndex, int toIndex)
          对指定 char 型数组的指定范围按数字升序进行排序。
static voidsort(double[] a)
          对指定的 double 型数组按数字升序进行排序。
static voidsort(double[] a, int fromIndex, int toIndex)
          对指定 double 型数组的指定范围按数字升序进行排序。
static voidsort(float[] a)
          对指定的 float 型数组按数字升序进行排序。
static voidsort(float[] a, int fromIndex, int toIndex)
          对指定 float 型数组的指定范围按数字升序进行排序。
static voidsort(int[] a)
          对指定的 int 型数组按数字升序进行排序。
static voidsort(int[] a, int fromIndex, int toIndex)
          对指定 int 型数组的指定范围按数字升序进行排序。
static voidsort(long[] a)
          对指定的 long 型数组按数字升序进行排序。
static voidsort(long[] a, int fromIndex, int toIndex)
          对指定 long 型数组的指定范围按数字升序进行排序。
static voidsort(Object[] a)
          根据元素的自然顺序对指定对象数组按升序进行排序。
static voidsort(Object[] a, int fromIndex, int toIndex)
          根据元素的自然顺序对指定对象数组的指定范围按升序进行排序。
static voidsort(short[] a)
          对指定的 short 型数组按数字升序进行排序。
static voidsort(short[] a, int fromIndex, int toIndex)
          对指定 short 型数组的指定范围按数字升序进行排序。
static
<T> void
sort(T[] a, Comparator<? super T> c)
          根据指定比较器产生的顺序对指定对象数组进行排序。
static
<T> void
sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
          根据指定比较器产生的顺序对指定对象数组的指定范围进行排序。
static StringtoString(boolean[] a)
          返回指定数组内容的字符串表示形式。
static StringtoString(byte[] a)
          返回指定数组内容的字符串表示形式。
static StringtoString(char[] a)
          返回指定数组内容的字符串表示形式。
static StringtoString(double[] a)
          返回指定数组内容的字符串表示形式。
static StringtoString(float[] a)
          返回指定数组内容的字符串表示形式。
static StringtoString(int[] a)
          返回指定数组内容的字符串表示形式。
static StringtoString(long[] a)
          返回指定数组内容的字符串表示形式。
static StringtoString(Object[] a)
          返回指定数组内容的字符串表示形式。
static StringtoString(short[] a)
          返回指定数组内容的字符串表示形式。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值