C#如何从我写的元素中删除任何元素。 [英] C# how to remove any element from the elements I wrote.

查看:60
本文介绍了C#如何从我写的元素中删除任何元素。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have DynamicArray class, i want write a function in class which will remove any element from the elements I wrote. For example`
DynamicArray d = new DynamicArray(4,5,6,1,2)
when i will call d.remove(2) function, it will remove number 6 and will print` 4,5,1,2





我的尝试:





What I have tried:

I don't know how write that function
public int remove()
{

}

推荐答案

我再次提到你上次提出这个问题: C#如何删除随机元素 [ ^ ]在那之前你被提醒过那个问题的解决方案: C# - 如何计算数组中元素的数量? [ ^ ]包含算法。



我们不是来做你的功课。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。

亲自尝试,您可能会发现它并不像您想象的那么困难。 />


因为如果你不这样做,你将永远不会学习如何正确思考并能够为自己做到这一点。这意味着你将失败你的课程,因为当你参加考试时,你将没有人为你写下...所以坐下来,开始阅读,开始思考 - 试一试,而不是期待别人去做它适合你:那不是开发的工作方式。
I refer you back to the last time you asked this question: C# how to remove random element[^] where you were reminded that the solution to teh question before that: C# - how can I count the number of elements in an array?[^] contains the algorithm.

We are not here to do your homework. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
Try it yourself, you may find it is not as difficult as you think.

Because if you don't, you will never learn how to "think correctly" and be able to do this for yourself. And that means you will fail your course, as when you get to the exam you will have no one to write it for you ... So sit down, start reading, start thinking - and give it a try instead of expecting others to do it for you: that isn;t how development works.


为什么不使用 List< int> ,参见这里的例子: [ Dotnetperls ]
Why don't you use List<int>, see example here: [Dotnetperls]


试试这样



try like this

public class DynamicArray {
    List<int> lstItems = new List<int>();
    public DynamicArray(params int[] items)
    {
        lstItems.AddRange(items);
    }
    public int[] Remove(int a) {
        lstItems.Remove(a);
        return lstItems.ToArray();
    }
}







DynamicArray d = new DynamicArray(4, 5, 6, 1, 2);
int[] items =  d.Remove(2); // 4,5,6,1
int[] items1 =  d.Remove(1); // 4,5,6
int[] items2 = d.Remove(4); // 5,6


这篇关于C#如何从我写的元素中删除任何元素。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆