一位数组中,数组的删除不太方便,可以对其进行替换
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace yy3
{
class Program
{
static void Main(string[] args)
{
/*
删除一维数组中指定值的元素,并排序后输出。
例如一维数组{19, 2, 4, 26, 2, 9, 52, 35, 2, 3 },
删除值为2的元素后数组为{19, 4, 26, 9, 52, 35, 3 },
排序后的数组为{3, 4, 9, 19, 26, 35, 52}。
*/
int[] arr = new int[] { 19, 2, 4, 26, 2, 9, 52, 35, 2, 3 };
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == 2)//如果值为2就替换为0
{
arr[i] = 0;
}
// Console.WriteLine(arr[i]);
}
//冒泡排序
for (int i = 0; i < arr.Length; i++)
{
for (int j = i+1; j < arr.Length; j++)
{
if (arr[i]>arr[j])
{
int temp = 0;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
Console.WriteLine(arr[i]);
}
/*
*
*
*
*
*
*
*
*
*
ArrayList arrayList = new ArrayList();
arrayList.Add(19);
arrayList.Add(2);
arrayList.Add(4);
arrayList.Add(26);
arrayList.Add(2);
arrayList.Add(9);
arrayList.Add(52);
arrayList.Add(35);
arrayList.Add(2);
arrayList.Add(3);
arrayList.Sort();
// arrayList.Remove();
foreach (var item in arrayList)
{
// int i = 0;
if (item.Equals(2))
{
// arrayList.Remove(1);
Console.WriteLine("0000000000000");
}
else
{
// Console.WriteLine(item);
}
// i++;
Console.WriteLine(item);
}
*/
}
}
}