使用阵列复制 [英] Use Array Copying

查看:61
本文介绍了使用阵列复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Java程序,其中一个包含5个Element的int数据类型的数组,例如10、15、20、25、30,并希望将所有相同的数据复制到另一个数组中而不放置相同的值. 任何帮助,请...

i have created a java program in which a array of int data type containing 5 Element like 10,15,20,25,30 and want to copying all the same data into Another array without putting same value.
any help please...

推荐答案

嘿,您可以使用Clone方法复制相同的元素来实现

作为

Hey you can do it by using Clone Method for copying same element

as

int[] arr1 = { 10, 15, 20, 25, 30 };
          int[] arr2 = (int[])arr1.Clone();  // Using Clone
          for (int i = 0; i < arr2.Length; i++)
          {
              System.out.println(arr2[i].ToString()); // Print arr2 value
          }
          Console.ReadLine();



当您编译并解释该程序时,您会注意到arr2将显示arr1的所有元素



when you compile and interprets this program you will notice that arr2 will display all element of arr1


好吧,如果您希望第二个数组是一个副本,那么根据定义,它应该具有相同的元素以相同的顺序.否则,您将创建一个包含一些任意元素的新数组.
Well if you want the 2nd array to be a copy, then by definition it should have the same elements in the same order. Otherwise you are just creating a new array with some arbitrary elements in it.


这篇关于使用阵列复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆