Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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()
{

}
Posted
Updated 18-Jan-18 20:56pm

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.
 
Share this answer
 
Comments
[no name] 19-Jan-18 2:59am    
5 out 5 for this answer.People should understand before asking questions if those are answered already by community members.I urge the community to vote for the solution given yesterday for a much needed question as i have resolved this question by using a sql Fiddle.

https://www.codeproject.com/Answers/1225902/How-do-I-get-the-below-output-in-sql-Please-help-m?cmt=970075#cmt970075
Why don't you use List<int>, see example here: [Dotnetperls]
 
Share this answer
 
Comments
Suren97 19-Jan-18 2:35am    
Need me write that function in class then call it in Main method
Suren97 19-Jan-18 2:37am    
here is show how write it in Main method
lukeer 19-Jan-18 4:11am    
That's because the List<whatever> class is already brought to you by the .NET framework.
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
 
Share this answer
 
Comments
Suren97 19-Jan-18 3:16am    
it's output Sysem.Int32[]
Karthik_Mahalingam 19-Jan-18 3:40am    
yes
Sysem.Int32 or int , both are same.
lukeer 19-Jan-18 4:16am    
By "it's output", you mean the result of d.ToString() or items.ToString(), or items1.ToString(), am I right?
If yes, then you need to write a function for that which iterates over all elements of the array and prints them. That's a different topic from removin an element from a n array.
Suren97 19-Jan-18 3:43am    
but Need me it output all numbers without removed number 4,5,1,2
Karthik_Mahalingam 19-Jan-18 3:45am    
i think you are talking about index ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900