最好的“推"方式进入C#数组 [英] Best way to "push" into C# array

查看:97
本文介绍了最好的“推"方式进入C#数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

所以我知道这是一个讨论得相当广泛的问题,但是我似乎找不到确切的答案.我知道您可以使用列表执行我要的操作,但这不能解决我的问题.

So I know that this is a fairly widely discussed issue, but I can't seem to find a definitive answer. I know that you can do what I am asking for using a List, but that won't solve my issue.

我用初学者C#授课.我有PHP和Java Script的背景,因此倾向于从这些语言方面进行思考.

I teach a class in beginner C#. I have a PHP and Java Script background, and thus tend to think in terms of these languages.

我一直在寻找向学生展示如何动态地向数组添加元素的最佳方法.我们不能使用列表,因为这些都不是我目前教的课程提纲的一部分(尽管它们确实来了)在本学期晚些时候.

I have been looking for the best way to show my students how to add elements to an array on the fly.We can't use Lists, as these are not part of the syllabus that I currently teach (although they do come in later in the semester).

因此,我正在寻找执行Java array.push(newValue)类型场景的最简单方法,这种方法对于新手程序员而言是可以理解的,而又不会教给他们不好的作法.

I am thus looking for the simplest way to perform a Java array.push(newValue) type scenario that will be understandable to novice coders without teaching them bad practice.

到目前为止,这是我一直在解决该问题的方式:

Here is how I have been approaching the problem so far:

for (int i = 0; i < myArray.Length; i++)
{
   if(myArray[i] == null)
   {
       myArray[i] = newValue;
       break;
   }
}

我只需要知道这是否是可以接受的方法,是否在教给他们任何不良做法,或者是否有更好/更简单的方法来实现自己的目标.

I just need to know if this is an acceptable approach, if I am teaching them any bad practices, or if there is a better / simpler way of achieving my goal.

编辑问题的症结在于,需要将元素添加到数组的第一个空插槽中,而这正是Java push函数可以做到的.

EDIT The crux of the matter is that the element needs to be added into the first empty slot in an array, lie a Java push function would do.

任何建议将不胜感激.

推荐答案

array.push 类似于 List< T> .Add ..NET数组是固定大小的,因此您实际上不能添加新元素.您所能做的就是创建一个比原始数组大一个元素的新数组,然后设置最后一个元素,例如

array.push is like List<T>.Add. .NET arrays are fixed-size so you can't actually add a new element. All you can do is create a new array that is one element larger than the original and then set that last element, e.g.

Array.Resize(ref myArray, myArray.Length + 1);
myArray[myArray.GetUpperBound(0)] = newValue;

对于此问题的修改,我不确定此答案是否确实适用:

I'm not sure that this answer actually applies given this edit to the question:

问题的症结在于该元素需要添加到一个数组中的第一个空插槽,就是一个Java push函数.

The crux of the matter is that the element needs to be added into the first empty slot in an array, lie a Java push function would do.

我提供的代码有效地附加了一个元素.如果目标是设置第一个空元素,则可以执行以下操作:

The code I provided effectively appends an element. If the aim is to set the first empty element then you could do this:

int index = Array.IndexOf(myArray, null);

if (index != -1)
{
    myArray[index] = newValue;
}

这是一个扩展方法,该方法封装该逻辑并返回放置值的索引;如果没有空元素,则返回-1.请注意,此方法也适用于值类型,将具有该类型默认值的元素视为空.

Here's an extension method that encapsulates that logic and returns the index at which the value was placed, or -1 if there was no empty element. Note that this method will work for value types too, treating an element with the default value for that type as empty.

public static class ArrayExtensions
{
    public static int Push<T>(this T[] source, T value)
    {
        var index = Array.IndexOf(source, default(T));

        if (index != -1)
        {
            source[index] = value;
        }

        return index;
    }
}

这篇关于最好的“推"方式进入C#数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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