数组工具类
来源:互联网 发布:ubuntu下安装wine 编辑:程序博客网 时间:2024/06/06 17:40
binarySearch(int[] a, int key)
使用二分搜索法来搜索指定的 int 型数组,以获得指定的值。必须在进行此调用之前对范围进行排序(通过sort(int[], int, int)
方法)。
例:import java.util.Arrays;
public class ArrayTest{
public static void main(Sting[] args){
int[] a = {1,2,3,4,5,6};
int b = Arrays.binarySearch(a,3);
System.out.println(b);
}
}
binarySearch(int[] a, int fromIndex, int toIndex, int key)
使用二分搜索法来搜索指定的 int 型数组的范围,以获得指定的值。
a
- 要搜索的数组fromIndex
- 要搜索的第一个元素的索引(包括)toIndex
- 要搜索的最后一个元素的索引(不包括)key
- 要搜索的值 public class ArrayTest
{
public static void main(String[] args){
int[] a ={1,2,3,4,5,6,7,8,9};
int b = Arrays.binarySearch(a,a[2],a[5],4);
System.out.println(b);
}
}
copyOf(int[] original, int newLength)
复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。
少了截取 ,多了补0
import java.util.Arrays;
public class ArrayTest
{
public static void main(String[] args){
int[] a = {1,2,3,4,5};
int[] b = Arrays.copyOf(a,3);
for(int c : b){
System.out.print(c);
}
}
}
copyOfRange(int[] original, int from, int to)
将指定数组的指定范围复制到一个新数组。
original
- 将要从其复制一个范围的数组from
- 要复制的范围的初始索引(包括)to
- 要复制的范围的最后索引(不包括)。(此索引可以位于数组范围之外)。public class ArrayTest
{
public static void main(String[] args){
int[] a = {1,2,3,4,5};
int[] b = Arrays.copyOfRange(a,1,7);
for(int c : b){
System.out.print(c);
}
}
}
equals(int[] a, int[] a2)
如果两个指定的 int 型数组彼此相等,则返回 true。
a
- 将测试其相等性的一个数组a2
- 将测试其相等性的另一个数组 import java.util.Arrays;
public class ArrayTest
{
public static void main(String[] args){
int[] a = {1,2,3,4,5};
int[] b = {1,2,3,4};
System.out.println(Arrays.equals(a,b));
}
}
fill(int[] a, int val)
将指定的 int 值分配给指定 int 型数组的每个元素。
参数:
a
- 要填充的数组
val
- 要存储在数组所有元素中的值
public class ArrayTest
{
public static void main(String[] args){
int[] a = new int[5];
Arrays.fill(a,10);
for(int b :a){
System.out.println(b);
}
}
}
fill(int[] a, int fromIndex, int toIndex, int val)
将指定的 int 值分配给指定 int 型数组指定范围中的每个元素。
a
- 要填充的数组fromIndex
- 要使用指定值填充的第一个元素的索引(包括)toIndex
- 要使用指定值填充的最后一个元素的索引(不包括)val
- 要存储在数组所有元素中的值 public class ArrayTest
{
public static void main(String[] args){
int[] a = new int[8];
Arrays.fill(a,2,8,10);
for(int b :a){
System.out.println(b);
}
}
}
sort(int[] a)
对指定的 int 型数组按数字升序进行排序。
import java.util.Arrays;
public class ArrayTest
{
public static void main(String[] args){
int[] a = {4,3,6,2,1,9,7,5};
Arrays.sort(a);
for(int b :a){
System.out.println(b);
}
}
}
sort(int[] a, int fromIndex, int toIndex)
对指定 int 型数组的指定范围按数字升序进行排序。
a - 要排序的数组
fromIndex
- 要排序的第一个元素的索引(包括) toIndex
- 要排序的最后一个元素的索引(不包括)public class ArrayTest
{
public static void main(String[] args){
int[] a = {4,3,6,2,1,9,7,5};
Arrays.sort(a,2,8);
for(int b :a){
System.out.println(b);
}
}
}
toString(int[] a)
返回指定数组内容的字符串表示形式。返回指定数组内容的字符串表示形式。字符串表示形式由数组的元素列表组成,括在方括号("[]")中。相邻元素用字符", "(逗号加空格)分隔。这些元素通过String.valueOf(int) 转换为字符串。如果 a 为null,则返回 "null"。
a
- 返回其字符串表示形式的数组 - java [数组]-[数组工具类]
- 数组Array工具类
- 数组工具类Arrays
- 数组处理工具类
- Arrays数组工具类
- 数组工具类
- byte[]数组工具类
- Arrays数组工具类
- 数组和工具类
- java数组工具类
- java数组工具类
- 数组、集合工具类
- 数组工具类
- Arrays数组工具类
- 数组排序,Arrays数组工具类专题(Arrays工具类)
- As3的数组工具类
- 工具类---数组操作ArrayUtil
- 数组工具类ArrayUtil.java
- 安卓自动化之uiautomator(python篇)二 UiDevice
- Android Studio查找功能(搜索功能)及快捷键
- python 字典中文key处理,读取,比较
- SLAM的傻瓜教程 SLAM for Dummies
- Maximum Sequence
- 数组工具类
- C++ 虚函数表解析
- 高并发与高可用浅析
- SpringBoot安装和初体验(A)
- Java反射
- File类以及文件过滤器
- Online Object Tracking : A Benchmark 翻译
- chenw Hibernate 缓存机制
- Python小记--二进制方式文件读写实践