| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2273 人关注过本帖
标题:arraylist用法,怎么无法显示排序???
只看楼主 加入收藏
仓乃梦
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:20
专家分:0
注 册:2016-10-3
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
arraylist用法,怎么无法显示排序???
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();
        }
    }
}
搜索更多相关主题的帖子: Random 
2016-10-24 15:45
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:20 
程序代码:
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();
        }
    }
}
2016-10-24 21:17
仓乃梦
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:20
专家分:0
注 册:2016-10-3
收藏
得分:0 
回复 2楼 yhlvht
这是昨天新教的内容,我们老师讲的有点快,知识点就没get到,谢啦,亲爱的版主大大。。。

立志成为版主一样的人。。。
2016-10-25 10:49
仓乃梦
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:20
专家分:0
注 册:2016-10-3
收藏
得分:0 
按照老师的要求和版主的帮助,润色了一下
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();
        }
    }
}

立志成为版主一样的人。。。
2016-10-25 11:14
快速回复:arraylist用法,怎么无法显示排序???
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019166 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved