C#实现数组内多个元素删除?可以实现一个元素的删除,但多于一个元素删除时,错误!
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceConsoleApplicat...
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication18
{
class container
{
private animal[] a;
private int num;
private int num1;
public container(int x)
{
num1 = 0;
num = x;
a = new animal[x];
}
public animal this[int index]
{
get
{
return a[index];
}
set
{
num1++;
a[index] = value;
}
}
public void println()
{
for (int i = 0; i < num1; i++)
{
a[i].print();
}
}
public void delete()
{
Console.WriteLine("\n请输入你要删除的元素个数!");
int m= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n请输入你要删除的元素的下标!");
for (int i = 0; i < m; i++)
{
int k= Convert.ToInt32(Console.ReadLine());
for (int j = 0; j < num1; j++)
{
if (j == k)
a[j] = null;
else
a[j].print();
}
}
}
}
abstract class animal
{
abstract public void print();
}
class tiger : animal
{
public override void print()
{
Console.Write("Tiger\t");
}
}
class horse : animal
{
public override void print()
{
Console.Write("Horse\t");
}
}
class duck : animal
{
public override void print()
{
Console.Write("Duck\t");
}
}
class pig : animal
{
public override void print()
{
Console.Write("Pig\t");
}
}
class sheep : animal
{
public override void print()
{
Console.Write("Sheep\t");
}
}
class Program
{
static void Main(String[] args)
{
container c = new container(5);
tiger t = new tiger();
horse h = new horse();
duck d = new duck();
pig p = new pig();
sheep s = new sheep();
c[0] = t;
c[1] = h;
c[2] = d;
c[3] = p;
c[4] = s;
c.println();
c.delete();
Console.ReadLine();
}
}
} 展开
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication18
{
class container
{
private animal[] a;
private int num;
private int num1;
public container(int x)
{
num1 = 0;
num = x;
a = new animal[x];
}
public animal this[int index]
{
get
{
return a[index];
}
set
{
num1++;
a[index] = value;
}
}
public void println()
{
for (int i = 0; i < num1; i++)
{
a[i].print();
}
}
public void delete()
{
Console.WriteLine("\n请输入你要删除的元素个数!");
int m= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n请输入你要删除的元素的下标!");
for (int i = 0; i < m; i++)
{
int k= Convert.ToInt32(Console.ReadLine());
for (int j = 0; j < num1; j++)
{
if (j == k)
a[j] = null;
else
a[j].print();
}
}
}
}
abstract class animal
{
abstract public void print();
}
class tiger : animal
{
public override void print()
{
Console.Write("Tiger\t");
}
}
class horse : animal
{
public override void print()
{
Console.Write("Horse\t");
}
}
class duck : animal
{
public override void print()
{
Console.Write("Duck\t");
}
}
class pig : animal
{
public override void print()
{
Console.Write("Pig\t");
}
}
class sheep : animal
{
public override void print()
{
Console.Write("Sheep\t");
}
}
class Program
{
static void Main(String[] args)
{
container c = new container(5);
tiger t = new tiger();
horse h = new horse();
duck d = new duck();
pig p = new pig();
sheep s = new sheep();
c[0] = t;
c[1] = h;
c[2] = d;
c[3] = p;
c[4] = s;
c.println();
c.delete();
Console.ReadLine();
}
}
} 展开
展开全部
到了现在,数组除了仅仅作为数组用,没有什么实际用处了,这些算法不研究也罢,过时了。
更多追问追答
追问
很多知识点都是有共性的,研究这些算法也是有好处的!问下:为什么你说数组没有什么实际用处了?
追答
数组的算法,是以前程序开发中的亮点,但是随着数据库技术的发展,这些算法都被新的数据库技术代替了,思想也不一样了,甚至说没共性了,研究数组容易阻碍你的视线。
实际程序开发中已经用不到数组的计算了,无论从效率还是算法,都不如数据库了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询