数组

数组

数组是相同类型元素的集合

数组类型

数组中的元素可以是基本数据类型,也可以是引用数据类型,数组本身是引用数据类型

数组的声明

int[] i;
int j[];

两种[]的放置的位置不同,但没有什么区别

只声明的数组是一个空指针,无法使用,需要创建后才可以使用

数组的创建

int[] i;
i=new int[3];

算了,不扯会的了。

数组的长度

数组名.length

字符串的长度:字符串名.length()

访问数组元素

根据数组的索引操作数组元素,索引从0开始

int[] a;
a=new int[3];
a[0]=1;
a[2]=100;
a[3]=1000;


for(int i=0;i<a.length;i++){
   System.out.println(a[i]);
}

for(int b: a){
   System.out.println(b);
}


数组的赋值

声明的同时创建

Employee[] eArray=new Employee[2];

声明的同时,用{}赋值

Employee  eArray2={1,2};

使用new[]{}直接赋值

int eArray3=new int[]{1,2};

二维数组

创建

复制数组

在JDK API的System类中,提供了一个数组复制的静态方法

arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

Array类

二分查找

public static int binarySearch(int[] a,int key)

填充

public static void fill(int[] a,int fromIndex,int toIndex,int val)

这里只是填充int型数组,api中还有填充其他数组的

排序

public static void sort(int[]  a,int fromIndex,int toIndex)

原文地址:https://www.cnblogs.com/aigeileshei/p/5453578.html