本文将给出三种实现数组复制的方法 (以复制整数数组为例)。
方法一 : 循环遍历赋值达到复制数组的效果
/**
* 循环遍历赋值达到复制数组的效果
*/
public static int [] copy1( int [] source) {
int len = source.length;
int [] result = new int [len];
for ( int i = 0 ; i < len; i++) {
result[i] = source[i];
}
return result;
} |
方法二 : 使用System.arraycopy复制数组
/** * 使用System.arraycopy复制数组
*/
public static int [] copy2( int [] source) {
int len = source.length;
int [] result = new int [len];
System.arraycopy(source, 0 , result, 0 , len);
return result;
} |
方法三 : 使用Arrays.copyOf复制数组
/** * 使用Arrays.copyOf复制数组
*/
public static int [] copy3( int [] source) {
int len = source.length;
int [] result = Arrays.copyOf(source, len);
return result;
} |
测试程序及结果如下:
int [] source = new int [] { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
System.out.println( "使用copy1方法" );
System.out.println(Arrays.toString(copy1(source))); System.out.println( "使用copy2方法" );
System.out.println(Arrays.toString(copy2(source))); System.out.println( "使用copy3方法" );
System.out.println(Arrays.toString(copy3(source))); |
使用copy1方法 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 使用copy2方法 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 使用copy3方法 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
原文链接:http://thecodesample.com/?cat=26
更多例子请访问 : http://thecodesample.com/
相关推荐
这三种方法中效率最高的是使用foreach语句遍历数组。从PHP4开始就引入了foreach结构,是PHP中专门为遍历数组而设计的语句,推荐大家使用。先分别介绍这几种方法。 一、使用for语句循环遍历数组 值得大家注意的是...
javascript 三种数组复制方法的性能对比,对于webkit, 使用concat; 其他浏览器, 使用slice.
● 复制数组:使用System.arraycopy()方法或Arrays.copyOf()方法将一个数组复制到另一个数组中。 ● 排序数组:使用Arrays.sort()方法对数组进行排序。 ● 搜索数组:使用Arrays.binarySearch()方法在一个已排序的...
一、冒泡排序法 说明:找到最大的数,排列到最后面,然后继续找 例: 复制代码 代码如下: $arr = array(3,5,-1,0,2); for($i=0;$i($j=0;$j>$arr[$j+1]){ $temp = $arr[$j]; $arr[$j]=$arr[$j+1]; $arr[$j+1]=$temp; ...
下面介绍PHP中遍历关联数组的三种方法: 方法1:foreach 复制代码 代码如下: <?php $sports = array( ‘football’ => ‘good’, ‘swimming’ => ‘very well’, ‘running’ => ‘not good’); foreach ($...
解释三、四种拷贝方式效率比较1. System.arraycopy( )2.Arrays.copyOf( )3.clone( )4.实测 一、深拷贝与浅拷贝解析 深拷贝与浅拷贝最大的区别就是拷贝过程中对象的属性是否指向的是同一个对象引用。 浅拷贝只是复制...
本文实例讲述了php从数组中随机选择若干不重复元素的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下:<?php /* * $array = the array to be filtered * $total = the maximum number of ...
1、Python的数组可分为三种类型: (1) list 普通的链表,初始化后可以通过特定方法动态增加元素。定义方式:arr = [元素] (2) Tuple 固定的数组,一旦定义后,其元素个数是不能再改变的。定义方式:arr = (元素) (2)...
concat var a = [1,2,3]; a.concat([4,5,6],7,8);//[1,2,3,4,5,6,7,8] 注意,a数组并没有改变,只是返回了一个新数组。 copyWithin ...// 将 3 号位复制到 0 号位 [1, 2, 3, 4, 5].copyWithin(0, 3, 4) //
这里提供三种方法来判断一个字符串中是否包括我们定义好的词,这比较适合于在留言,评论等地址进行关键词过滤,实例代码如下: 复制代码 代码如下:$crud = array(‘中国|||我国|||大地’, ‘kelon|||lerke|||sb’, ...
Python中有三种拷贝方式分别是浅拷贝、深拷贝和赋值拷贝,这篇文章通过实例代码给大家介绍了Python的三种拷贝方式,需要的朋友可以参考下
快速复制数组的一部分的实用方法 in是 System类中一个InputStream类的对象.提供 输入数据的方法,最常 用的read方法有三个: read0:从输人流中读取数据的下一个字节。 read(byte[]b):从输入流中读取一定数量的...
1.用unset()方法:复制代码 代码如下:<?php$a=array(“red”, “green”, “blue”, “yellow”); count($a); //得到4 unset($a[1]); //删除第二个元素 count($a); //得到3 echo $a[2]; //数组中仅有三个元素...
② 数组段不会复制原数组的元素,但原数组可以通过ArraySegment中的Array属性访问, 如果数组段中的元素改变了,则这些变化就会反映到原数组 2.一个例子 private int SumOfSegments(ArraySegment<
1.自己编写函数,将两个字符串进行复制 代码如下:#include<iostream>using namespace std;int main(){ char str1[]=”I love China!”,str2[20]; void Strcpy(char *p1,char *p2); Strcpy(str2,str1); cout<&...
PHP的数组,数组中的内容大致如下:复制代码 代码如下:$list = array( array(2,3,5), array(2,5,24), array(3,8,6), array(3,2,10), array(4,7,20), array(4,1,15), array(6,4,10), array(7,9,20), );...
php在数组中查找指定值是否存在的方法有很多,记得很久以前我一直都是傻傻的用foreach循环来查找的,下面我主要分享一下用php内置的三个数组函数来查找指定值是否存在于数组中,这三个数组分别是 in_array(),array_...
定义store方法,实现入库处理,即将定义好的卡片对象的信息复制到卡片数组中;方法头如下: public void store(ref Card card) { 。。。。。。 } 定义show方法,实现卡片信息的输出; 另外定义测试类,包括静态...