C# 可变二维数组做参数参数问题..

如何解决传三个参数的动态数组问题
[问题点数:40分]
如何解决传三个参数的动态数组问题
[问题点数:40分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2013年3月 C/C++大版内专家分月排行榜第三
2014年10月 C/C++大版内专家分月排行榜第三2014年4月 C/C++大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。编写高质量代码改善C#程序的157个建议[动态数组、循环遍历、对象集合初始化]
  & 软件开发过程中,不可避免会用到集合,C#中的集合表现为数组和若干集合类。不管是数组还是集合类,它们都有各自的优缺点。如何使用好集合是我们在开发过程中必须掌握的技巧。不要小看这些技巧,一旦在开发中使用了错误的集合或针对集合的方法,应用程序将会背离你的预想而运行。
建议16、元素数量可变的情况下不应使用数组
  在C#中,数组一旦被创建,长度就不能改变。如果我们需要一个动态且可变长度的集合,就应该使用ArrayList或List&T&来创建。而数组本身,尤其是一维数组,在遇到要求高效率的算法时,则会专门被优化以提升其效率。一维数组也成为向量,其性能是最佳的,在IL中使用了专门的指令来处理它们。
  从内存使用的角度来讲,数组具有以下特点:
  1、数组在创建时被分配了一段固定长度的内存。
  2、如果数组元素是值类型,则每个元素的长度等于相应的值类型的长度
  3、如果数组的元素是引用类型,则每个元素的长度为该引用类型的IntPtr.Size。
  4、数组的存储结构一旦被分配,就不能再变化。
  而ArryaList是这样的:
  1、ArrayList是链表结构,可以动态增减内存空间。
  2、如果ArrayList存储的是值类型,则会为每个元素增加12字节的空间,其中4字节用于对象引用,8字节是元素装箱时引入的对象头。
  而List&T&是ArrayList的泛型实现,它省去了拆箱和装箱带来的开销。
如果一定要动态改变数组的长度,一种方法是将数组转换为ArrayList或List&T&,如下面的代码所示:
& & & & & & ///定义一个一维数组
& & & & & & int[] iArr = { 0,1,3,4,6,7,9};
& & & & & & ///将数组转换为ArrayList
& & & & & & ArrayList arrayListInt = ArrayList.Adapter(iArr);
& & & & & & arrayListInt.Add(11);
& & & & & & ///将数组转换为List&T&
& & & & & & List&int& listInt = iArr.ToList&int&();
& & & & & & listInt.Add(11);
  还有一种方法是用数组的复制功能。数组继承自System.Array,抽象类System.Array提供了一些有用的实现方法,其中就包含了Copy方法,它负责将一个数组的内容复制到另外一个数组中。无论是哪种方法,改变数组长度就相当于重新创建了一个数组对象。
  为了让数组看上去本身就具有动态改变长度的功能,还可以创建一个名为ReSize的扩展方法。
& & public static class ClassForExtensions
& & & & public static Array ReSize(this Array array,int newSize)
& & & & & & Type t = array.GetType().GetElementType();
& & & & & & Array newArray = Array.CreateInstance(t, newSize);
& & & & & & Array.Copy(array, 0, newArray, 0, Math.Min(array.Length, newSize));
& & & & & & return newA
调用方式如下:
& & & & static void Main(string[] args)
& & & & & & int[] iArr = { 0,1,3,4,6,7,9};
& & & & & & iArr = (int[])ClassForExtensions.ReSize(iArr, 20);
& & & & & & Console.ReadLine();
下面我们来对比一下性能,先来看代码:
& & class Program
& & & & static void Main(string[] args)
& & & & & & ResizeArray();
& & & & & & ResizeList();
& & & & & & Console.ReadLine();
& & & & public static void ResizeArray()
& & & & & & int[] iArr = {0,1,3,4,6,8 };
& & & & & & Stopwatch watch = new Stopwatch();
& & & & & & watch.Start();///用于测量时间间隔
& & & & & & iArr = (int[])iArr.ReSize(10);
& & & & & & watch.Stop();///
& & & & & & Console.WriteLine(&ResizeArray:{0}&, watch.Elapsed);
& & & & public static void ResizeList()
& & & & & & List&int& iArr = new List&int&(new int[] { 0, 1, 3, 4, 6, 8, 9 });
& & & & & & Stopwatch watch = new Stopwatch();
& & & & & & watch.Start();
& & & & & & iArr.Add(0);
& & & & & & iArr.Add(0);
& & & & & & iArr.Add(0);
& & & & & & watch.Stop();
& & & & & & Console.WriteLine(&ResizeList:{0}&, watch.Elapsed);
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'共有 2533 人关注过本帖
标题:做一个可变数组 在数组中输入几个数 求和?
等 级:新手上路
&&问题点数:0&&回复次数:7&&&
做一个可变数组 在数组中输入几个数 求和?
做一个可变数组 想在数组中输入几个数就输入几个数 求和?
搜索更多相关主题的帖子:
等 级:新手上路
帖 子:762
你前面的贴子已经有人回复了
请不要重复发贴
等 级:新手上路
帖 子:401
public class Sum_Test
&public static void Main()
&&int sum=0;
&&Console.WriteLine (&你想算几个数的和:(0-100)&);
&&int i_number=int.Parse (Console.ReadLine ());
&&int[] A=new int [i_number];
&&for(int i=0;i&i_i++)
&&&Console.WriteLine (&请输入第{0}个数:&,i+1);
&&&A[i]=int.Parse (Console.ReadLine ());
&&&sum+=A[i];
&&Console.WriteLine (&这{0}个数的和是{1}:&,i_number,sum);
声明:我这是盗版
我是初学者,希望大家能多多帮助我
screen.width*0.7) {this.resized= this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized= this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {} else {window.open('http://bbs.bc-cn.net/bbs/showimg.asp?BoardID=34&filename=.gif');}" onmousewheel="return imgzoom(this);" alt="" />
等 级:新手上路
帖 子:49
难兄难弟呀~~!!!!
我不是高手,我只是新手~!!
等 级:贵宾
威 望:10
帖 子:226
专家分:20
怎么都是软件学院的,这个论坛成我们软件学院的了,,
等 级:新手上路
帖 子:401
以下是引用冰封谷主在 7:58:13的发言:
怎么都是软件学院的,这个论坛成我们软件学院的了,,
你们是什么软件学院啊
我是初学者,希望大家能多多帮助我
screen.width*0.7) {this.resized= this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized= this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {} else {window.open('http://bbs.bc-cn.net/bbs/showimg.asp?BoardID=34&filename=.gif');}" onmousewheel="return imgzoom(this);" alt="" />
等 级:新手上路
帖 子:93
等 级:新手上路
帖 子:303
以下是引用yushengou在 9:55:02的发言:
你们是什么软件学院啊
是啊, 没办法啊!
衣带渐宽终不悔,
为伊消得人憔悴。
纸上得来终觉浅,
绝知此事要躬行。
版权所有,并保留所有权利。
Powered by , Processed in 0.030143 second(s), 8 queries.
Copyright&, BCCN.NET, All Rights Reserved<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&1 楼:评论arr.Length 好像求的是总长度吧

我要回帖

更多关于 二维数组做参数 的文章

 

随机推荐