黑马程序员技术交流社区

标题: 数组的疑问 [打印本页]

作者: 邢凯    时间: 2014-8-4 18:09
标题: 数组的疑问
本帖最后由 邢凯 于 2014-8-5 10:13 编辑

怎么把一个刚产生的数字放到一个新的数组中,这个新的数组没有定义过长度。这个要怎么实现
作者: ╃→梅飛揚之城    时间: 2014-8-4 18:54
//设置一个list集合
            List<int> list = new List<int>();
            
            int a = 12;
            int b = 23;
            //添加到集合中
            list.Add(a);
            list.Add(b);
            //将集合转换为数组,数组的长度都可以改变了,需要往数组添加,只需添加到list中就行。
            int[] arr = list.ToArray();
            for (int i = 0; i < arr.Length; i++) {
                Console.WriteLine("数组下标{0}的值是{1}", i, arr[i]);
            }
作者: 许庭洲    时间: 2014-8-4 19:59
using System;
public class MyClass
{
       public static void UseParams(params int[] list)
       {
               for(int i=0;i<list.Length;i++)
               {
                       Console.WriteLine(list[i]);
               }
               Console.WriteLine();
        }
       public static void UseParams2(params object[] list)
       {
               for(int i=0;i<list.Length;i++)
               {
                       Console.WriteLine(list[i]);
               }
               Console.WriteLine();
        }
        static void Main()
        {
                UseParams(1,2,3);
                UseParams(1,2,3,4,5);
                UseParams2(1,'a',"test");
                UseParams2(1,'a',"test","Chinese","English");
                int[] myarray=new int[3]{10,11,12};
                UseParams(myarray);
          }
}

/----------------------------------程序输出结果-------------------------------------------/
1
2
3

1
2
3
4
5

1
a
test

1
a
test
Chinese
English

10
11
12
/----------------------------------程序输出结果-------------------------------------------/


作者: 邢凯    时间: 2014-8-5 00:58
╃→梅飛揚之城 发表于 2014-8-4 18:54
//设置一个list集合
            List list = new List();
            

List<int> list = new List<int>();  这个是定义一个新数组吗?我好想没在基础视频里面看到过啊
作者: 邢凯    时间: 2014-8-5 01:01
许庭洲 发表于 2014-8-4 19:59
using System;
public class MyClass
{

大哥,你的这个好深奥啊,看不懂啊
作者: lc6897094    时间: 2014-8-5 15:52
先把数字转换成和数组内元素一样的数据类型,然后赋值给 数组名[下标]
作者: tangkun3126    时间: 2014-8-5 19:35
C#中的数组是不能动态改变大小的,也就是说,数组的维度和维度长度一旦定义了,就不能改变。在这种情况时你可以使用ArrayList集合,或使用 List<T>泛型集合
作者: 倚楼听雨    时间: 2014-8-5 20:00
List<int> list = new List<int>();  创建




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2