java 字符串数组拷贝_Java数组声明与拷贝的几种方式

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[] 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[] 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[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));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值