注册 登录
编程论坛 C# 论坛

arraylist用法,怎么无法显示排序???

仓乃梦 发布于 2016-10-24 15:45, 2472 次点击
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace array
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            
            Random rd = new Random(DateTime .Now .Second );
            ArrayList alist = new ArrayList();
            for (i = 0; i < 20; i++)
            {
                alist.Add(rd.Next (60,100));
                if (i % 10 == 0)
                {
                    alist.Add('\n');
                }
            }
            foreach (object ob in alist)
            {
                 Console.Write("{0,4}",ob );
            }
            alist.Sort();
            for (int j = 0; j < alist.Count; j++)
            {
                alist .Add (alist[j ]);
            }
            foreach (object obj in alist)
            {
                Console.Write("{0,4}", obj);
            }
           
            Console.Read();
        }
    }
}
3 回复
#2
yhlvht2016-10-24 21:17
程序代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace array
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;

            Random rd = new Random(DateTime.Now.Second);
            ArrayList alist = new ArrayList();
            for (i = 0; i < 20; i++)
            {
                alist.Add(rd.Next(60, 100));
                //ArrayList中既含有数字,又含有字符,是不能排序的,数字和字符无法比较
               
//if (i % 10 == 0)
               
//{
               
//    alist.Add('\n');
               
//}
            }
            foreach (object ob in alist)
            {
                Console.Write("{0,4}", ob);
            }
            alist.Sort();
            //排完序以后,alist里面的值已经是排完序以后的状态,排序以前的顺序已经没有了
            
//for (int j = 0; j < alist.Count; j++)
            
//{
            
//    alist.Add(alist[j]);
            
//}
            foreach (object obj in alist)
            {
                Console.Write("{0,4}", obj);
            }
            Console.Read();
        }
    }
}
#3
仓乃梦2016-10-25 10:49
回复 2楼 yhlvht
这是昨天新教的内容,我们老师讲的有点快,知识点就没get到,谢啦,亲爱的版主大大。。。
#4
仓乃梦2016-10-25 11:14
按照老师的要求和版主的帮助,润色了一下
ArrayList应用:产生随机数排序并输出
(1)随机产生20个整数,在60-100之间,加入ArrayList对象中;
(2)每行10个数据,输出这些随机数;
(3)从小到大排序,输出排序后的随机数。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace array
{
    class Program
    {
        static void Main(string[] args)
        {
            int i,count=0;
            Random rd = new Random(DateTime.Now.Second);
            ArrayList alist = new ArrayList();
            for (i = 0; i < 20; i++)
            {
                alist.Add(rd.Next(60, 100));                           
            }
            foreach (object ob in alist)
            {
                Console.Write("{0,4}", ob);
                count ++;
                if (count % 10 == 0)
                {
                    Console.WriteLine();
                }
            }
            alist.Sort();
            Console.WriteLine("排序后的数为:");
            foreach (object obj in alist)
            {            
                Console.Write("{0,4}", obj);
                count++;
                if (count % 10 == 0)
                {
                    Console.WriteLine();
                }
            }
             Console.Read();
        }
    }
}
1