获取数组的索引并删除该索引剩下的所有内容

发布于 2024-11-02 04:20:34 字数 171 浏览 2 评论 0原文

我有一个 int 数组,

int[] RowOfints = 1,2,3,4,5,6,7,8,9;

如果我输入例如值 4,我想从数组中删除 1,2,3 并返回剩下的内容。 怎么做呢?

I have an array of ints

int[] RowOfints = 1,2,3,4,5,6,7,8,9;

if i enter for example value 4 i want to remove 1,2,3 from array and return what's left.
How to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

呆橘 2024-11-09 04:20:34

如果您不想使用 LINQ:

int[] newRowOfInts = new int[RowOfInts.Length - index];
Array.Copy(RowOfInts, index, newRowOfInts, 0, newRowOfInts.Length);

If you don't want to use LINQ:

int[] newRowOfInts = new int[RowOfInts.Length - index];
Array.Copy(RowOfInts, index, newRowOfInts, 0, newRowOfInts.Length);
≈。彩虹 2024-11-09 04:20:34

在 LINQ 中使用 Skip 扩展。

int[] newArray = RowOfInts.Skip(value).ToArray();

Using Skip extension in LINQ.

int[] newArray = RowOfInts.Skip(value).ToArray();
初心 2024-11-09 04:20:34

我正在解释您的问题,即您想要找到值 4 的索引,然后从该索引位置开始获取所有内容。

var result = RowOfInts.SkipWhile(item => item != 4); // optionally, .ToArray()

result 将是由 4 .. 9 组成的 IEnumerable。如果您想要一个具体的数组,您也可以使用可选的 ToArray() 扩展方法。如果数组中没有元素符合给定的条件,您将得到一个零长度序列。

I'm interpreting your question that you want to find the index for the value 4 and then take everything starting from that index position.

var result = RowOfInts.SkipWhile(item => item != 4); // optionally, .ToArray()

result will be an IEnumerable<int> consisting of 4 .. 9. If you want a concrete array, you can use the optional ToArray() extension method as well. If no elements in the array match the given criteria, you will get a zero-length sequence.

无尽的现实 2024-11-09 04:20:34

好的,既然我更好地理解了这个问题,我将发布我的实际需求版本(再次反常地强调效率而不是可读性):

private static int[] RemoveBeforeValue(int[] source, int value)
{
    if (source == null)
        return null;
    int valueIndex = 0;
    while (valueIndex < source.Length && source[valueIndex] != value)
        valueIndex++;
    if (valueIndex == 0)
        return source;
    int[] result = new int[source.Length - valueIndex];
    Array.Copy(source, valueIndex, result, 0, result.Length);
    return result;
}

旧答案

如果你想努力做到这一点(但高效!)方式,那么你可以这样做(假设你想删除小于提供值的值):

private static int[] RemoveValuesLessThan(int[] source, int newMinimum)
{
    if (source == null)
        return null;
    int lessThanCount = 0;
    for (int index = 0; index < source.Length; index++)
        if (source[index] < newMinimum)
            lessThanCount++;
    if (lessThanCount == 0)
        return source;
    int[] result = new int[source.Length - lessThanCount];
    int targetIndex = 0;
    for (int index = 0; index < source.Length; index++)
        if (source[index] >= newMinimum)
            result[targetIndex++] = source[index];
    return result;
}

OK, now that I understand the question better, I will post my version of the actual requirements (again perversely emphasising effeciency over readability):

private static int[] RemoveBeforeValue(int[] source, int value)
{
    if (source == null)
        return null;
    int valueIndex = 0;
    while (valueIndex < source.Length && source[valueIndex] != value)
        valueIndex++;
    if (valueIndex == 0)
        return source;
    int[] result = new int[source.Length - valueIndex];
    Array.Copy(source, valueIndex, result, 0, result.Length);
    return result;
}

OLD ANSWER

If you want to do it the hard (but efficient!) way, then you can do this (assuming you want to remove values less than the supplied value):

private static int[] RemoveValuesLessThan(int[] source, int newMinimum)
{
    if (source == null)
        return null;
    int lessThanCount = 0;
    for (int index = 0; index < source.Length; index++)
        if (source[index] < newMinimum)
            lessThanCount++;
    if (lessThanCount == 0)
        return source;
    int[] result = new int[source.Length - lessThanCount];
    int targetIndex = 0;
    for (int index = 0; index < source.Length; index++)
        if (source[index] >= newMinimum)
            result[targetIndex++] = source[index];
    return result;
}
小女人ら 2024-11-09 04:20:34

输出
对于具有此类型的连续整数数组,

public static void RemoveIntsBefore(int i) 
    {
        int[] RowOfints = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        for (int k = 0; k < RowOfints.Length; k++)
        {
            if (RowOfints.ElementAt(k) < i)
            {
                RowOfints[k] = i;
            }
        }

        RowOfints = RowOfints.Distinct().ToArray();
//this part is to write it on console
            //foreach (var item in RowOfints)
            //{
            //    Console.WriteLine(item);
            //}

            //Console.ReadLine();

    }

您的数组不必是连续的

public static void RemoveIntsBefore(int i) 
    {
        int[] RowOfints = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1,2 };                     

        Console.WriteLine("OUTPUT");
        foreach (var item in Enumerable.Range(i-1, RowOfints.Length + 1 - i).ToArray())
        {
            Console.WriteLine(RowOfints[item]);
        }

        Console.ReadLine();

    }

output
For a sequential array of ints

public static void RemoveIntsBefore(int i) 
    {
        int[] RowOfints = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        for (int k = 0; k < RowOfints.Length; k++)
        {
            if (RowOfints.ElementAt(k) < i)
            {
                RowOfints[k] = i;
            }
        }

        RowOfints = RowOfints.Distinct().ToArray();
//this part is to write it on console
            //foreach (var item in RowOfints)
            //{
            //    Console.WriteLine(item);
            //}

            //Console.ReadLine();

    }

with this one your array does not have to be sequential

public static void RemoveIntsBefore(int i) 
    {
        int[] RowOfints = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1,2 };                     

        Console.WriteLine("OUTPUT");
        foreach (var item in Enumerable.Range(i-1, RowOfints.Length + 1 - i).ToArray())
        {
            Console.WriteLine(RowOfints[item]);
        }

        Console.ReadLine();

    }
娇柔作态 2024-11-09 04:20:34

使用 System.Linq;
....

int[] RowOfints = {1,2,3,4,5,6,7,8,9};
int[] Answer = RowOfints.Where(x => x != 1 && x != 2 && x != 3).ToArray()

using System.Linq;
....

int[] RowOfints = {1,2,3,4,5,6,7,8,9};
int[] Answer = RowOfints.Where(x => x != 1 && x != 2 && x != 3).ToArray()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文