Arrays tools common method


Complete C language self-study manual (33)

Android multi-resolution framework for adaptation

JavaWeb core technology tutorial series

HTML5 front-end development combat tutorial series

MySQL database gymnastics tutorials (35 graphical version)

And overthrow their own past - Custom View tutorial series (10)

Thinking out of the woods, set foot on the road --Android develop sophisticated Advanced essence record

Android programmers looking to tell the front tutorial series (40 episodes free video tutorials + source code)


Copyright Notice

  • This article original author: Columbia Valley's younger brother
  • On the blog address: http: //blog.csdn.net/lfdfhl

Outline

In the Java API provides the tools java.util.Arrays operation of the array, for example: sort, search, copy, array to a set of array to a string and other common operations. This article describes a method commonly used Arrays.

Examples

package com.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 本文作者:谷哥的小弟 
 * 博客地址:http://blog.csdn.net/lfdfhl
 * 
 * Arrays工具类使用示例
 * 
 */
public class TestArrays {

	public static void main(String[] args) {
		TestArrays testArrays=new TestArrays();
		testArrays.test();
	}
	
	public void test() {
		//将数组转换为List
		String[] stringArray1 = {"lucy","lili","dada"};
		List<String> list = Arrays.asList(stringArray1);
		System.out.println(list);
		
		//将数组转换为字符串
		int[] intArray1= {9,5,2,7};
		String string = Arrays.toString(intArray1);
		System.out.println(string);
		
		//对数组排序
		int[] intArray2= {9,5,2,7};
		Arrays.sort(intArray2);
		System.out.println(Arrays.toString(intArray2));
		
		//判读数组是否相等
		int[] intArray3= {7,5,2,7};
		int[] intArray4= {5,9,2,7};
		boolean isEquals = Arrays.equals(intArray3, intArray4);
		System.out.println(isEquals);
		
		//利用二分法查找元素
		int[] intArray5= {5,9,2,7};
		Arrays.sort(intArray5);
		int index = Arrays.binarySearch(intArray5, 9);
		System.out.println("index="+index);
		
		//拷贝数组中某范围的数据
		int[] intArray6= {0,1,2,3,4,5,6};
		int[] intArray7 = Arrays.copyOfRange(intArray6, 0, 5);
		System.out.println(Arrays.toString(intArray7));
		
	}

}

result

Here Insert Picture Description

Released 1022 original articles · won praise 1986 · Views 2.38 million +

Guess you like

Origin blog.csdn.net/lfdfhl/article/details/104577099