为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > Java数组复制的方法

Java数组复制的方法

2018-04-11 21页 doc 61KB 5阅读

用户头像

is_314871

暂无简介

举报
Java数组复制的方法Java数组复制的方法 数组的复制方法现在看来至少有四个思路: 1 使用循环结构 这种方法最灵活。唯一不足的地方可能就是代码较多 2 使用Object类的clone() 方法, 这种方法最简单,得到原数组的一个副本。灵活形也最 差。效率最差,尤其是在数组元素很大或者复制对象数组时。 3 使用Systems的arraycopy 这种方法被告之速度最快,并且灵活性也较好,可以指定原 数组名称、以及元素的开始位置、复制的元素的个数,目标数组名称、目标数组的位置。 4 Arrarys类的copyOf()方法与copyOfRa...
Java数组复制的方法
Java数组复制的方法 数组的复制方法现在看来至少有四个思路: 1 使用循环结构 这种方法最灵活。唯一不足的地方可能就是代码较多 2 使用Object类的clone() 方法, 这种方法最简单,得到原数组的一个副本。灵活形也最 差。效率最差,尤其是在数组元素很大或者复制对象数组时。 3 使用Systems的arraycopy 这种方法被告之速度最快,并且灵活性也较好,可以指定原 数组名称、以及元素的开始位置、复制的元素的个数,目标数组名称、目标数组的位置。 4 Arrarys类的copyOf()方法与copyOfRange()方法可实现对数组的复制。 例一: public class CopyTester { /** * @param args */ public static void main(String[] args) { int[][] nums = { { 10, 20 }, { 30, 40, 50 }, { 60 } }; int i, j; int[][] result = new int[nums.length][]; // 一个一个拿,一个一个输出 for (i = 0; i < nums.length; i++) { result[i] = new int[nums[i].length]; for (j = 0; j < nums[i].length; j++) { result[i][j] = nums[i][j]; System.out.print(result[i][j] + " "); } System.out.println(); } // 每行复制输出 for (i = 0; i < nums.length; i++) { result[i] = new int[nums[i].length]; result[i] = java.util.Arrays.copyOfRange(nums[i], 0, nums[i].length); System.out.println(java.util.Arrays.toString(result[i])); } System.out.println("----------------"); result[2][0]=100; for( i=0;i< nums.length;i++){ for( j=0;j< nums[i].length;j++) System.out.print(nums[i][j]+" "); System.out.println(); } for( i=0;i< result.length;i++){ layout development cities in developed countries show that developing rail transit Guide and an important means of achieving sustainable urban development. More than the bulk of the urban rail transit vehicles, rail traffic in the urban space layout and guide the evacuation, optimizing the structure and other aspects also play an important role in its role as the scale becomes more important. Rail transit on land development and stimulation is achieved through its good accessibility, under the dual role of mechanisms in land and planning, transport accessibility high land along the rail transit development and high strength, promotion of urban morphology and land use patterns to adjust accordingly. 1, and Copenhagen 1947, Copenhagen refers to shaped planning introduced, requirements land development along narrow of finger corridor concentrated, refers to shaped planning guide big Copenhagen area along track traffic line clear defined of corridor for Metro development, through track traffic and city development of integration, will along development concentrated in track station around a km around, around track site land implementation integrated intensive development, and provides good of non-mobile of way received connection facilities, Ensure that a larger proportion of the region's residents commute using mass transit. Figure 5.1-3 Copenhagen Metro City Development 2, Tokyo 5.1-4 Tokyo Metro to guide regional development (left: 1950, right: 2000) formation is the mass transit system of the city of Tokyo, which have been developed under the guidance of. Eastern suburbs ... Established to track traffic based on the network-city, using rail-oriented intensive development mode has been recognized in many cities. Created the urban structure of the node, forming the urban rail transit-dependent and lifestyle, as well as mass transit systems provide adequate passenger protection. 5.1.3 to guide the healthy development of for( j=0;j< result[i].length;j++) System.out.print(result[i][j]+" "); System.out.println(); } } } 运行: C:\java>java CopyTester 10 20 30 40 50 60 [10, 20] [30, 40, 50] [60] ---------------- 10 20 30 40 50 60 10 20 30 40 50 100 修改result[0][2] 的值并没有影响原数组。 由输出可以看出, 例二: import java.util.Arrays; public class arraycopy { /** * @author x521 * @createdate 2008-5-21 * 一维数组的拷贝---浅拷贝 */ public static void main(String[] args) { copyarray(); copyobject(); } public static void copyarray() { int[] a = new int[5]; int[] b = new int[6]; a[0] = 12; a[1] = 45; layout development cities in developed countries show that developing rail transit Guide and an important means of achieving sustainable urban development. More than the bulk of the urban rail transit vehicles, rail traffic in the urban space layout and guide the evacuation, optimizing the structure and other aspects also play an important role in its role as the scale becomes more important. Rail transit on land development and stimulation is achieved through its good accessibility, under the dual role of mechanisms in land and planning, transport accessibility high land along the rail transit development and high strength, promotion of urban morphology and land use patterns to adjust accordingly. 1, and Copenhagen 1947, Copenhagen refers to shaped planning introduced, requirements land development along narrow of finger corridor concentrated, refers to shaped planning guide big Copenhagen area along track traffic line clear defined of corridor for Metro development, through track traffic and city development of integration, will along development concentrated in track station around a km around, around track site land implementation integrated intensive development, and provides good of non-mobile of way received connection facilities, Ensure that a larger proportion of the region's residents commute using mass transit. Figure 5.1-3 Copenhagen Metro City Development 2, Tokyo 5.1-4 Tokyo Metro to guide regional development (left: 1950, right: 2000) formation is the mass transit system of the city of Tokyo, which have been developed under the guidance of. Eastern suburbs ... Established to track traffic based on the network-city, using rail-oriented intensive development mode has been recognized in many cities. Created the urban structure of the node, forming the urban rail transit-dependent and lifestyle, as well as mass transit systems provide adequate passenger protection. 5.1.3 to guide the healthy development of a[2] = 90; a[3] = 100; a[4] = 160; for(int aa : a) { System.out.print(aa + " "); } System.out.println("\n ---"); System.arraycopy(a, 0, b, 1, a.length); for(int bb : b) { System.out.print(bb + " "); } System.out.println(); System.out.println("************"); a[4] = 200;//这个修改不会影响b[4] for(int bbb : b) { System.out.print(bbb + " "); } System.out.println("\n*************"); for(int aaa : a) { System.out.print(aaa + " "); } System.out.println("\n****************"); } public static void copyobject() { testcopy[] tc1 = new testcopy[2]; testcopy[] tc2 = new testcopy[2]; tc1[0] = new testcopy(); tc1[1] = new testcopy(); tc1[0].setName("h1"); tc1[1].setName("h2"); for(testcopy co : tc1) { System.out.print(co.getName() + " "); layout development cities in developed countries show that developing rail transit Guide and an important means of achieving sustainable urban development. More than the bulk of the urban rail transit vehicles, rail traffic in the urban space layout and guide the evacuation, optimizing the structure and other aspects also play an important role in its role as the scale becomes more important. Rail transit on land development and stimulation is achieved through its good accessibility, under the dual role of mechanisms in land and planning, transport accessibility high land along the rail transit development and high strength, promotion of urban morphology and land use patterns to adjust accordingly. 1, and Copenhagen 1947, Copenhagen refers to shaped planning introduced, requirements land development along narrow of finger corridor concentrated, refers to shaped planning guide big Copenhagen area along track traffic line clear defined of corridor for Metro development, through track traffic and city development of integration, will along development concentrated in track station around a km around, around track site land implementation integrated intensive development, and provides good of non-mobile of way received connection facilities, Ensure that a larger proportion of the region's residents commute using mass transit. Figure 5.1-3 Copenhagen Metro City Development 2, Tokyo 5.1-4 Tokyo Metro to guide regional development (left: 1950, right: 2000) formation is the mass transit system of the city of Tokyo, which have been developed under the guidance of. Eastern suburbs ... Established to track traffic based on the network-city, using rail-oriented intensive development mode has been recognized in many cities. Created the urban structure of the node, forming the urban rail transit-dependent and lifestyle, as well as mass transit systems provide adequate passenger protection. 5.1.3 to guide the healthy development of } System.out.println(); System.arraycopy(tc1, 0, tc2, 0, tc1.length); for(testcopy tc : tc2) { System.out.print(tc.getName() + " "); } //重新设置tc2[1]的值 后 tc1[1]的值 也发生了变化 // 这种情况属于 数组的浅拷贝问题 tc2[1].setName("hello"); System.out.println(); for(testcopy tc11 : tc1) { System.out.print(tc11.getName() + " "); } System.out.println("\n----"); } } class testcopy { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } } 运行: C:\java>java arraycopy 12 45 90 100 160 --- 0 12 45 90 100 160 ************ 0 12 45 90 100 160 ************* 12 45 90 100 200 layout development cities in developed countries show that developing rail transit Guide and an important means of achieving sustainable urban development. More than the bulk of the urban rail transit vehicles, rail traffic in the urban space layout and guide the evacuation, optimizing the structure and other aspects also play an important role in its role as the scale becomes more important. Rail transit on land development and stimulation is achieved through its good accessibility, under the dual role of mechanisms in land and planning, transport accessibility high land along the rail transit development and high strength, promotion of urban morphology and land use patterns to adjust accordingly. 1, and Copenhagen 1947, Copenhagen refers to shaped planning introduced, requirements land development along narrow of finger corridor concentrated, refers to shaped planning guide big Copenhagen area along track traffic line clear defined of corridor for Metro development, through track traffic and city development of integration, will along development concentrated in track station around a km around, around track site land implementation integrated intensive development, and provides good of non-mobile of way received connection facilities, Ensure that a larger proportion of the region's residents commute using mass transit. Figure 5.1-3 Copenhagen Metro City Development 2, Tokyo 5.1-4 Tokyo Metro to guide regional development (left: 1950, right: 2000) formation is the mass transit system of the city of Tokyo, which have been developed under the guidance of. Eastern suburbs ... Established to track traffic based on the network-city, using rail-oriented intensive development mode has been recognized in many cities. Created the urban structure of the node, forming the urban rail transit-dependent and lifestyle, as well as mass transit systems provide adequate passenger protection. 5.1.3 to guide the healthy development of **************** h1 h2 h1 h2 h1 hello ---- 例三、 import java.util.Arrays; public class twoarraycopy { /** * @author x521 * @createdate 2008-5-21 * 二维数组的拷贝---浅拷贝 */ public static void main(String[] args) { int[][] a = new int[2][3]; int[][] b = new int[2][3]; for(int i=0; i<2; i++) { for(int j=0; j<3; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } System.out.println("****************"); a[0][2] = 100; for(int[] aa : a) { System.out.println(Arrays.toString(aa)); } System.out.println("****************"); System.arraycopy(a, 0, b, 0, a.length);//这个是浅拷贝 // for (int i = 0; i < a.length; i++) { // b[i] = new int[a[i].length]; // b[i] = java.util.Arrays.copyOfRange(a[i], 0, a[i].length); // } //修改a[1][2]的值 ,并且b[1][2]的值也被改变 a[1][2] = 500; layout development cities in developed countries show that developing rail transit Guide and an important means of achieving sustainable urban development. More than the bulk of the urban rail transit vehicles, rail traffic in the urban space layout and guide the evacuation, optimizing the structure and other aspects also play an important role in its role as the scale becomes more important. Rail transit on land development and stimulation is achieved through its good accessibility, under the dual role of mechanisms in land and planning, transport accessibility high land along the rail transit development and high strength, promotion of urban morphology and land use patterns to adjust accordingly. 1, and Copenhagen 1947, Copenhagen refers to shaped planning introduced, requirements land development along narrow of finger corridor concentrated, refers to shaped planning guide big Copenhagen area along track traffic line clear defined of corridor for Metro development, through track traffic and city development of integration, will along development concentrated in track station around a km around, around track site land implementation integrated intensive development, and provides good of non-mobile of way received connection facilities, Ensure that a larger proportion of the region's residents commute using mass transit. Figure 5.1-3 Copenhagen Metro City Development 2, Tokyo 5.1-4 Tokyo Metro to guide regional development (left: 1950, right: 2000) formation is the mass transit system of the city of Tokyo, which have been developed under the guidance of. Eastern suburbs ... Established to track traffic based on the network-city, using rail-oriented intensive development mode has been recognized in many cities. Created the urban structure of the node, forming the urban rail transit-dependent and lifestyle, as well as mass transit systems provide adequate passenger protection. 5.1.3 to guide the healthy development of for(int[] bb : b) { System.out.println(Arrays.toString(bb)); } System.out.println("****************"); for(int[] aa : a) { System.out.println(Arrays.toString(aa)); } } } C:\java>java twoarraycopy 0 0 0 0 0 0 **************** [0, 0, 100] [0, 0, 0] **************** [0, 0, 100] [0, 0, 500] **************** [0, 0, 100] [0, 0, 500] 例四: import java.util.*; public class TestArray { public static void main(String[] args) { Map map=new HashMap(); List list=new ArrayList(); int[] arr=new int[]{1,2,3,4,5}; // 方法 1 使用for 循环结构 int[] arr1=new int[arr.length]; long time11=System.currentTimeMillis(); for(int j=0;j< 21474836;j++){ for(int i=0;i< arr1.length;i++){ arr1[i]=arr[i]; } } long time12=System.currentTimeMillis(); layout development cities in developed countries show that developing rail transit Guide and an important means of achieving sustainable urban development. More than the bulk of the urban rail transit vehicles, rail traffic in the urban space layout and guide the evacuation, optimizing the structure and other aspects also play an important role in its role as the scale becomes more important. Rail transit on land development and stimulation is achieved through its good accessibility, under the dual role of mechanisms in land and planning, transport accessibility high land along the rail transit development and high strength, promotion of urban morphology and land use patterns to adjust accordingly. 1, and Copenhagen 1947, Copenhagen refers to shaped planning introduced, requirements land development along narrow of finger corridor concentrated, refers to shaped planning guide big Copenhagen area along track traffic line clear defined of corridor for Metro development, through track traffic and city development of integration, will along development concentrated in track station around a km around, around track site land implementation integrated intensive development, and provides good of non-mobile of way received connection facilities, Ensure that a larger proportion of the region's residents commute using mass transit. Figure 5.1-3 Copenhagen Metro City Development 2, Tokyo 5.1-4 Tokyo Metro to guide regional development (left: 1950, right: 2000) formation is the mass transit system of the city of Tokyo, which have been developed under the guidance of. Eastern suburbs ... Established to track traffic based on the network-city, using rail-oriented intensive development mode has been recognized in many cities. Created the urban structure of the node, forming the urban rail transit-dependent and lifestyle, as well as mass transit systems provide adequate passenger protection. 5.1.3 to guide the healthy development of System.out.println(time11); System.out.println(time12); System.out.println(time12-time11); list.add(time12-time11); map.put("for",time12-time11); TestArray.print_Array(arr1); System.out.println("==============================="); // 方法 2 使用Object的 protected Object clone() 方法 int[] arr2=new int[arr.length]; long time21=System.currentTimeMillis(); for(int j=0;j< 21474836;j++){ arr2=(int[])arr.clone(); // arr2=(arr.clone(); // 这里也可以直接这么写 } long time22=System.currentTimeMillis(); System.out.println(time21); System.out.println(time22); System.out.println(time22-time21); list.add(time22-time21); -time21); map.put("clone",time22 TestArray.print_Array(arr2); System.out.println("==============================="); 使用System 的 public static void arraycopy(Object src, int // 方法 3 srcPos, Object dest, int destPos,int length) 方法 int[] arr3=new int[arr.length]; long time31=System.currentTimeMillis(); for(int j=0;j< 21474836;j++){ System.arraycopy(arr, 0, arr3, 0, arr.length); } long time32=System.currentTimeMillis(); System.out.println(time31); System.out.println(time32); System.out.println(time32-time31); list.add(time32-time31); map.put("arraycopy",time32-time31); TestArray.print_Array(arr3); System.out.println("==============================="); System.out.println(list); System.out.println(map); } layout development cities in developed countries show that developing rail transit Guide and an important means of achieving sustainable urban development. More than the bulk of the urban rail transit vehicles, rail traffic in the urban space layout and guide the evacuation, optimizing the structure and other aspects also play an important role in its role as the scale becomes more important. Rail transit on land development and stimulation is achieved through its good accessibility, under the dual role of mechanisms in land and planning, transport accessibility high land along the rail transit development and high strength, promotion of urban morphology and land use patterns to adjust accordingly. 1, and Copenhagen 1947, Copenhagen refers to shaped planning introduced, requirements land development along narrow of finger corridor concentrated, refers to shaped planning guide big Copenhagen area along track traffic line clear defined of corridor for Metro development, through track traffic and city development of integration, will along development concentrated in track station around a km around, around track site land implementation integrated intensive development, and provides good of non-mobile of way received connection facilities, Ensure that a larger proportion of the region's residents commute using mass transit. Figure 5.1-3 Copenhagen Metro City Development 2, Tokyo 5.1-4 Tokyo Metro to guide regional development (left: 1950, right: 2000) formation is the mass transit system of the city of Tokyo, which have been developed under the guidance of. Eastern suburbs ... Established to track traffic based on the network-city, using rail-oriented intensive development mode has been recognized in many cities. Created the urban structure of the node, forming the urban rail transit-dependent and lifestyle, as well as mass transit systems provide adequate passenger protection. 5.1.3 to guide the healthy development of // 写一个打印数组中所有元素的方法 public static void print_Array(int[] arr){ for(int i=0;i< arr.length;i++){ System.out.println("[ "+i+" ] = " +arr[i]); } } } layout development cities in developed countries show that developing rail transit Guide and an important means of achieving sustainable urban development. More than the bulk of the urban rail transit vehicles, rail traffic in the urban space layout and guide the evacuation, optimizing the structure and other aspects also play an important role in its role as the scale becomes more important. Rail transit on land development and stimulation is achieved through its good accessibility, under the dual role of mechanisms in land and planning, transport accessibility high land along the rail transit development and high strength, promotion of urban morphology and land use patterns to adjust accordingly. 1, and Copenhagen 1947, Copenhagen refers to shaped planning introduced, requirements land development along narrow of finger corridor concentrated, refers to shaped planning guide big Copenhagen area along track traffic line clear defined of corridor for Metro development, through track traffic and city development of integration, will along development concentrated in track station around a km around, around track site land implementation integrated intensive development, and provides good of non-mobile of way received connection facilities, Ensure that a larger proportion of the region's residents commute using mass transit. Figure 5.1-3 Copenhagen Metro City Development 2, Tokyo 5.1-4 Tokyo Metro to guide regional development (left: 1950, right: 2000) formation is the mass transit system of the city of Tokyo, which have been developed under the guidance of. Eastern suburbs ... Established to track traffic based on the network-city, using rail-oriented intensive development mode has been recognized in many cities. Created the urban structure of the node, forming the urban rail transit-dependent and lifestyle, as well as mass transit systems provide adequate passenger protection. 5.1.3 to guide the healthy development of
/
本文档为【Java数组复制的方法】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索