java data structure in an array of common methods -API

java data structure in an array of common methods -API

Conversion arrays and collections

The integer array into a collection, you must use the original type. Can not be directly int [] into a set of parameters as asList () method must be the object. You should first int [] into Integer []. For other types of primitive array, too, it must be converted to a corresponding array of packaging types.

Into a list

Arrays.asList () returns a list of fixed-size supported by the specified array. So do not Add, Remove and other operations.

List list = new ArrayList (Arrays.asList (userid)); this operation on it.

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 an array of turn

Object[] objs = strList.toArray();

If you want to become a String array, require strong transfer type.

String[] strs = (String[]) strList.toArray(new String[0]);

You can also specify the size:

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());  
	}

How can I see the array contains an element

	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 class binarySearch () method with the specified array --- searches the binary search algorithm --- returns the index to search for all types of element array ---

----- array must be sorted before the call (by the sort () method) --- If the array contains multiple elements with the specified value is not guaranteed to find which one

1)Arrays.binarySearch(Object[], Object key);

If the key contained in the array, the index value is returned; otherwise, "-1" or "-" (insertion point)

The insertion point: search key to be inserted that point, that is the first element of the array index greater than the key

2)Arrays.binarySearch(Object[], int fromIndex,int toIndex,Object key);

To the search range from fromIndex (toIndex-1)

Exception: If the length of the specified range of greater than or equal to the array, will be reported abnormal 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);
        
	}

Replace Array

	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());
	}

 

Array merge

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());
	}

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_35029061/article/details/92076624
Recommended