java数组中的APL的使用

java数组中的APL的使用

JAVA中的数组,数组中的API

1、API是什么? 2、数组常用的API 3、foreach遍历数组

1、API是什么?

应用程序的接口,系统提供的预定义函数,每个函数都会有特定的功能,直接调用即可,不需要知道它的内部细节

2.API帮助文档:

1)需要找到特定的类-—-不同的包 2)研究类的功能和层次关系。 3)找到特定的API函数-一函数如何进行使用?

3.具体的使用

1)目前使用的都是静态方法–static修饰的方法,直接通过类名调用Arrays.toString(实参值)

2、数组常用的API

1).使用Arrays.toString输出所有数组元素,后面传入数组地址值

System.out.println("排序前的数组为:");
	System.out.println(Arrays.toString(a));

2).数组的排序——API

Arrays.sort(a);//调用sort方法对数组进行排序
	//Arrays.sort(a, 0, 3);//数组的部分元素排序、左闭右开
	System.out.println("排序后的数组为:");
	System.out.println(Arrays.toString(a));

3).数组元素的查找,二分查找(折半查找)——前提是该数组有序

Arrays.sort(a);
	//System.out.println("查找后的结果是:"+Arrays.binarySearch(a, 4));
	if(Arrays.binarySearch(a, 2)>0){
          
   
		System.out.println("查找后的结果是:"+Arrays.binarySearch(a, 2));
	}else{
          
   
		System.out.println("该元素不存在");
	}

4).使用API判断两个数组是否相等

int x[]={
          
   1,2,3,4,5};
	int y[]=new int[a.length];
	System.out.println(Arrays.equals(x, y));

5).使用API实现数组的复制System.arrayCopy

五个参数 Object src,int srcPos,Object dest,int destPos, int length

System.arraycopy(x, 0, y, 0, 5);
	System.out.println(Arrays.toString(y));

3、foreach遍历数组

//foreach——只能读取,不能改写
	static void print(int arrays[]){
          
   
	//定义方法遍历数组
		/*for(int i=0;i<=arrays.length;i++){
			System.out.println(arrays[i]);
		}*/
	//foreach——只能读取,不能改写
		for (int b:arrays)
			System.out.println(b);
	}
经验分享 程序员 微信小程序 职场和发展