数组操作-去除重复和空白元素

        /// <summary>
        /// 字符串数组转化为整形数组 并进行冒泡排序
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private int[] ArrToInt(string[] str)
        {
            int[] arr2 = new int[str.Length];   //用来存放将字符串转换成int[]
            for (int i = 0; i < str.Length; i++)
            {
                arr2[i] = int.Parse(str[i]);
            }
            int len = arr2.Length;
            for (int j = 0; j < len; ++j)
            {
                for (int i = j; i < len; ++i)
                {
                    if (arr2[j] > arr2[i])
                    {
                        int temp = arr2[j];
                        arr2[j] = arr2[i];
                        arr2[i] = temp;
                    }
                }
            }

            return arr2;
        }
        /// <summary>
        /// 重组数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string[] strTo(string [] str)
        {
            ArrayList al = new ArrayList();//去除数组中的重复元素
            for (int i = 0; i < str.Length; i++)
            {
                if (al.Contains(str[i]) == false && str[i] != "")
                {
                    al.Add(str[i]);
                }
            }

            //把ArrayList转换数组 
            str = new string[al.Count];
            str = (string[])al.ToArray(typeof(string));
            return str;

        }

 

posted @ 2016-01-05 11:06  请叫我七哥  阅读(547)  评论(0编辑  收藏  举报