Array, how to erase an element and resize it

AlamaFromBe

Member
Joined
Jan 27, 2020
Messages
12
Programming Experience
10+
Hello,

I would like to be able to delete an element from an array and then reduce its size to the new size while keeping all the other elements.
I'm having a little trouble with the Net5 Framework, I'm looking for something like:
Example:
int myValue = myArrayObject.popAnResize(index X);
Which Classes would you suggest to me?
 
It would have been better if you had a List<T> or List so that you could call Remove() to get the effect that you want. The basic Array doesn't have such a method. You will have to create a brand new array with the correct new size and copy items over from the old array into the new array skipping the item that you wish to remove.
 
As a quick folllow-up you could also use Array.Resize() to avoid having to allocate a new array explicitly, but you would still be responsible for shuffling the array items around before you shrink the size of the array. (Under the covers, Array.Resize() allocates another array for you. See Reference Source.)
 
Last edited:
Both of those suggestions are terrible Skydiver. Although your suggestions fit with what they want, you are also doing right in telling them to use an appropriate collection type which provides the functionality for adding and removing of objects.

Depending on what it is they are storing, they could use a Queue<T>, which might be better than a list for them if they are already working with data transferred from an array. Once an item is dequeued, it is removed from the Queue<T>.

Using a Stack<T> similarly to Queue<T>, you can pop one from the stack. And both of these make working with arrays easier to use if the data is already coming from an array to either of these two.
Maybe not better than a List<T>, but if the OP is doing what it sounds like. (Likely iterating the array and looking to remove one object at a time if its used during the iteration cycle, then a Stack<T> is what I'd choose as they can peek at the next item, and pop it off if they want to use it or not.)

The are also others you can use. You find them here : System.Collections.Generic Namespace

Perhaps if you told us more about what you are doing, we could give you better advice?
 
Agree it's terrible, but it's what they asked for. And give the naming convention that they used for their prototype signature, it looked like something from either AS3, or some Java-esque style of programming. In both cases, the OP would have been expecting to gave the C/C++ like behavior where no new allocation occurs, but unfortunately in C#, a new allocation will have to occur if they want to truly use an array.

Unfortunately, neither the Queue<T> nor the Stack<T> will let you remove an item at any given index which is what the OP seems to be asking based on prototype signature he provided. Queues will always remove from the front, while stacks will always remove from the top.
 
Unfortunately, neither the Queue<T> nor the Stack<T> will let you remove an item at any given index which is what the OP seems to be asking based on prototype signature he provided. Queues will always remove from the front, while stacks will always remove from the top.
Correct (y)

The reasons I mentioned them is because I wasn't sure what they were actually doing. It's kinda hard to go on the little bit that they shared. And so I was hoping to throw those collection types at them in case its a direction they want to go with their code. After all when they wrote :
popAnResize(index X);
I assumed they might actually want to pop and remove an item. Maybe I'm wrong, but I think the OP should tell us in detail what they are doing, and if they do want to use a specific index, then they will need to consider using a different collection type. The closest to popping and removing, is the Stack<T>. Rather than argueing semantics, when we might both be wrong, they should explain what they are doing first. ;)

As a side note; I miss the days where people knew how to ask questions the smart and elaborate way.
 
To make matters worse, Alexander Stepanov conflated arrays, lists, and stacks in his original STL implementation. He chose to use the verbs, push and pop to mean appending and removing items from the end of a C-style array that he wrapped with his vector<> class. From a talk I heard, his original intent was for a vector to be used in multiple ways and prevent the proliferation of specialized classes -- e.g. it was not meant to be a computer scientist's object oriented abstract data type, but rather an computer engineer's "C++ is merely C with classes" tool. And now we get people using the phrase "push to an array/list" when they actually mean append to an array/list.
 
Hello guys, thank you both. This is my first time using .Net with C #, I am an AS3 programmer and there are quite a few differences between the two frameworks. But I'm trying to learn to use .Net to do Blazor afterwards.
I suspected that there was probably better than the Array class to do what I'm looking for. like a list or a collection.
The goal is simple, there are 100 numbers from 1 to 100, we draw a number at random and we should not be able to draw the same one twice like a lottery.


Maybe I should have started by explaining that, but I don't like to ask for a ready-made answer, I like to be referred sometimes helped, but not that we write the code for me, except big galley. :)
So the right question is which class would be ideal for this?
 
Last edited:
No. That class is mostly around from the early days before generics were introduced (as well as to help Java programmers transition into C#). Also you'll be hitting boxing and unboxing performance issues storing integers into it. The best class for your purposes is Queue<int>, since eventually you'll just be pulling the next number.

So now how does on populate the queue with numbers from 1 to 100 shuffled? You pass in an array of integers (int []) or a List<int> where the numbers have been shuffled. Fisher-Yates is a good shuffling algorithm.

Also, note that I'm being careful about using the word shuffle or shuffling instead of saying random. With your definition of random, then that would mean that if I toss a coin and I get heads, the next time I toss that same coin, I should get tails.
 
Last edited:
C#'s ArrayList is to AS3's Array, as C# List<T> is to AS3's Vector.
 
Yes! Vector in AS3, A vector is an array of the same type and faster , I see List<T> in .Net :)
Ok, thank for your explanation.

I had already started on the basis of ArrayList, I understand that it is not great! ok, .Net is so vast ..: |
At the moment I am writing this, but it doesn't matter if I have to start all over again, I have to learn even if the beginnings are very difficult.


Fisrt test:
using System;
using System.Collections;


public class SamplesArrayList
{

    public static void Main()
    {

        /*_____________________________________________________________________________________________
     
                                         Creates and initializes a new ArrayList.
          _____________________________________________________________________________________________*/

        ArrayList myAL = new ArrayList();
        for (int i = 1; i <= 100; i++)
        {
            myAL.Add(i);
            Console.WriteLine(myAL[i - 1]);
        }
        Console.WriteLine("Panier contain :" + myAL.Count + "numbers");

        /*_____________________________________________________________________________________________
     
                                                   get random 1 value
          _____________________________________________________________________________________________*/

        Random rnd = new Random();
        int indexMyArList = rnd.Next(0, myAL.Count);
        Console.WriteLine("index: " + indexMyArList);
        myAL.RemoveAt(indexMyArList);

        Console.WriteLine("Panier contient :" + myAL.Count + "nombres");


    }//end class
}//end MyApplication

Either way I'm missing the read instruction in the array, I'm going to give that up to try what you say. :)
 
Last edited:
Back
Top Bottom