您的位置:首页 > 编程语言 > Java开发

Java数组声明与拷贝的几种方式

2016-04-25 23:27 477 查看
Java数组声明的三种方式

第一种(声明并初始化):

数据类型[] 数组名={值,值,...};

例:int[] a = {1,2,3,4,5,6,7,8};

第二种(声明后赋值):

数据类型[] 数组名 = new 数据类型[数组长度];

数组名[下标1]=值;数组名[下标2]=值;.....

例:String[] a =new String[4];

a[0]="hello";

a[1]="world";

....

第三种():

数据类型[] 数组名=new 数据类型[]{值,值,...};

Java数组拷贝的四种方式

第一种:for循环自己手写,注意数组越界异常

第二种: System.arraycopy(src, srcPos, dest, destPos, length);

Object src,int srcPos,Object dest,int descPos,int length

源数组,源数组开始拷贝位置,目标数组,目标数组开始拷贝位置,拷贝长度

第三种:java.util.Arrays.Arrays.copyOf(源数组,新数组长度);

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}

这个方法有两个参数,第一个参数是源数组,可以数byte,short,int,long,char,float,double,boolean

第二个参数是新数组长度

该方法先new一个新数组,然后拷贝,返回值是一个数组,需要先声明一个数组存放返回数组,也可以直接用Arrays.toString(Arrays.copyOf(a,a.length))遍历

java.util.Arrays.copyOfRange(源数组,开始拷贝位置,结束拷贝位置);

源数组的数据类型和Arrays.copyOf();一样

public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}

第四种:clone();

int []h=new int[c.length];
h=(int[])c.clone();
h=c.clone();//h与c同数据类型


Demo:

package syntax;
import java.util.Arrays;
public class ArrayDemo2 {

public static void main(String[] args) {

int[] a={1,2,3,4,5,6,7,8};

int[] b=new int[10];
b[1]=1;
b[2]=2;

int[] c=new int[]{1,2,3,4,5,34,6,7,8,9,12};
System.out.println("数组a="+Arrays.toString(a));
System.out.println("数组b="+Arrays.toString(b));
System.out.println("数组c="+Arrays.toString(c));

System.out.println("\n===System.arraycopy拷贝===");
System.out.println("===把数组c拷贝到长度为a.length的数组d===");
//数组的copy
int[] d=new int[a.length];
System.arraycopy(c, 0, d, 0, a.length);
//从a数组的第0个位置开始复制a.length个数据,复制到数组d第0个位置开始
System.out.println("数组d="+Arrays.toString(d));

System.out.println("\n===Arrays.copyOf拷贝===");
//先new一个新的数组,再将原来数组拷贝到新数组中,原数组引用与其不同
System.out.println("===把数组a拷贝到长度为a.length+1的数组e===");
int[] e=Arrays.copyOf(a, a.length+1);
//从a数组的第0个元素开始复制,复制长度为a.length+1;默认初始化值为0
System.out.println("数组e="+Arrays.toString(e));

System.out.println("\n=========for循环拷贝=========");
System.out.println("===把数组c拷贝到长度为10的数组f===");
//for循环复制
int []f=new int[10];
for(int i=0;i<f.length;i++){
f[i]=c[i];
}
System.out.println("数组f="+Arrays.toString(f));

System.out.println("\n=========clone循环拷贝=========");
System.out.println("===把数组c拷贝到长度为c.length的数组h===");
int []h=new int[c.length];
h=(int[])c.clone();
System.out.println("数组h="+Arrays.toString(h));
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: