How to remove the first item from an array without for or foreach

Asked

Viewed 1,270 times

0

How to remove the first item from an array without for or foreach.

I tried this, but it says that there is no method to remove or remove.

int[] arr = {1,2,3,4,5};

var removeu = Array.Remove(arr,0);//Agora percebi que isso é javascript, rs

This is a mistake. How do I do it? I passed it to a list, but it was a mistake too.

List<int> lista = new List<int>(arr);

lista.Remove(0); // Zero here not the first index.

Like I do now?

  • 1

    tries to use removeAt

  • 1

    list.Removeat(0);

  • The problem with Removeat is that it is void and I cannot assign the value removed like this: var first = list.Removeat(0); This gives error.

  • 1

    If you want to remove the element, that’s it, if you want to assign the element before removing it, just do it before removing it

  • @pnet solved the problem?

  • Solved, not in the proposed way I wanted but resolves, both answers solve. I know, still close the post. I will close yes.

Show 1 more comment

3 answers

5


  • Continues to give error: Cannot assign void to an implicitly-typed local variable

  • I need to assign the value being removed to a variable.

  • 1

    use list<int> and see if it works

  • Note: to use the arr.First(), should import the namespace System.Linq in the archive.

3

According to the comment what you want to do is this:

var primeiro = arr.RemoveAt(0);

Only that one array does not have the method RemoveAt then you have to create an extension method like this out of the OS:

public static T[] RemoveAt<T>(this T[] source, int index) {
    T[] dest = new T[source.Length - 1];
    if(index > 0)
        Array.Copy(source, 0, dest, 0, index); 
    if(index < source.Length - 1)
        Array.Copy(source, index + 1, dest, index, source.Length - index - 1);
    return dest;
}

I put in the Github for future reference.

  • That may be, but it’s a test I’m doing and the proposed applicators have given me a single-time example of assignment and removal. That’s why I insist on trying it once.

  • It starts that if that’s what you need, you need to explain it better in the question. I don’t see any other way to do it simply. This is doing in a single time.

  • this post is part of another that I did, so the part about how to do it at once, but it’s okay, I see that there is no other way. The other post is this: how to implement a function to return the sum of the squares of the proposed form

1

If you will always add elements at the end and remove from the beginning, you can use the class Queue, which deals with queue (first in first out), that is, whenever it is going to be removed it will be from the beginning, follows an example:

        Queue<int> fila= new Queue<int>();
        fila.Enqueue(1); //adiciona valor 1
        fila.Enqueue(2); //adiciona valor 2
        fila.Enqueue(3); //adiciona valor 3
        fila.Enqueue(4); //adiciona valor 4
        fila.Enqueue(5); //adiciona valor 5

        fila.Dequeue();//remove o primeiro elemento, neste caso o 1

If this is your case, the best data structure to use is this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.