java数据结构-API中数组常用方法
数组与集合的转换
将整形数组转为集合,必须用原始类型。不能直接将int[]转化为集合、因为asList()方法的参数必须是对象。应该先把int[]转化为Integer[]。对于其他原始的类型的数组也是如此,必须先转换成相应的包装类类型数组。
转化为list
Arrays.asList()返回一个受指定数组支持的固定大小的列表。所以不能做Add、Remove等操作。
List list = new ArrayList(Arrays.asList(userid));这样操作就可以了。
public class Array {
public static void main(String[] args) {
String[] str = new String[]{"zyy", "zxx", "zww"};
List<String> list = new ArrayList<>(Arrays.asList(str));
list.add("whh");
System.out.println("ArrayList是否包含:" + list.contains("whh"));
Set<String> set = new HashSet<>(Arrays.asList(str));
System.out.println("集合set是否包含:" + set.contains("wyy"));
}
}
public static void main(String[] args) {
String[] userid = {"aa","bb","cc"};
List<String> userList = new ArrayList<String>();
Collections.addAll(userList, userid);
System.out.println(userList.toString());
}
list转数组
Object[] objs = strList.toArray();
如果要变成String数组,需要强转类型。
String[] strs = (String[]) strList.toArray(new String[0]);
也可以指定大小:
String[] strs = strList.toArray(new String[strList.size()]);
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
String[] arr = (String[]) list.toArray(new String[list.size()]);
System.out.println(arr.toString());
}
如何查看数组是否包含某个元素
public static void main(String[] args) {
String[] str = new String[]{"zyy", "zxx", "zww"};
System.out.println("字符串是否包含:" + Arrays.toString(str).contains("zyy"));
System.out.println("字符串是否包含:" + Arrays.toString(str).contains("wyx"));
}
Arrays类的binarySearch()方法---用二分搜索法搜索指定数组---返回要搜索元素的索引值---适用于各种类型数组
-----必须在调用之前对数组进行排序(通过sort()方法)---如果数组包含多个带有指定值的元素,则无法保证找到的是哪一个
1)Arrays.binarySearch(Object[], Object key);
如果key包含在数组中,返回索引值;否则返回“-1”或“-”(插入点)
插入点:搜索键将要插入数组的那一点,即第一个大于此键的元素索引
2)Arrays.binarySearch(Object[], int fromIndex,int toIndex,Object key);
搜索范围为从fromIndex到(toIndex-1)
异常:如果指定的范围为大于或等于数组的长度,则会报出ArrayIndexOutOfBoundsException异常
public static void main(String[] args) {
String str[] = new String[]{"ab","cd","ef","gh"};
Arrays.sort(str); //必须先将数组排序
int index1 = Arrays.binarySearch(str,"cd");
int index2 = Arrays.binarySearch(str,"de");
//返回插入点
int index3 = Arrays.binarySearch(str,0,2,"cd");
System.out.println(index1);
System.out.println(index2);
System.out.println(index3);
}
替换数组
public static void main(String[] args) {
int arr1[] = new int[5];
int arr2[] = new int[5];
Arrays.fill(arr1,7); //使用同一个值对数组进行填充替换
Arrays.fill(arr2,0,3,7); //指定范围
System.out.println(arr1.toString());
System.out.println(arr2.toString());
}
合并数组
System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制。
其函数原型是:
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)1
src:源数组;
srcPos:源数组要复制的起始位置;
dest:目的数组;
destPos:目的数组放置的起始位置;
length:复制的长度。
注意:src and dest都必须是同类型或者可以进行转换类型的数组.
public static void main(String[] args) {
Integer[] a = { 1, 2, 3, 4, 5 };
Integer[] b = { 6, 7, 8, 9, 10 };
Integer[] c = new Integer[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
System.out.println(c.toString());
}
public static void main(String[] args) {
//定义长度为 5 的数组
int scores[]=new int[]{57,81,68,75,91};
//定义一个新的数组,将scores数组中的5个元素复制过来
//同时留3个内存空间供以后开发使用
int[] newScores=(int[])Arrays.copyOf(scores,8);
System.out.println("\n复制的新数组内容如下:"+newScores.toString());
}
public static void main(String[] args) {
int scores[]=new int[]{57,81,68,75,91,66,75,84};
//复制源数组的前5个元素到newScores数组中
int newScores[]=(int[])Arrays.copyOfRange(scores,0,5);
System.out.println("\n复制的新数组内容如下:");
System.out.println("\n复制的新数组内容如下:"+newScores.toString());
}
public static void main(String[] args) {
//定义源数组,长度为8
Integer scores[]=new Integer[]{100,81,68,75,91,66,75,100};
//复制数组,将Object类型强制转换为int[]类型
Integer newScores[]=(Integer[])scores.clone();
System.out.println("\n复制的新数组内容如下:"+newScores.toString());
}