I'm currently trying to remove the first element in an int array using objects in C#. I have just coded how to add an element to that start of the array and though it was just a case of changing a few things.
Here's the code for the addFirst() but i can't for the life of me figure out removeFirst() - public void addFirst(int value)
-
{
-
if (isFull())
-
{
-
throw new Exception("List full");
-
}
-
else
-
{
-
for (int pos = size;pos > 0; pos-- )
-
{
-
values[pos] = values[pos - 1];
-
}
-
values[0] = value;
-
size++;
-
}
-
}
2 14386
What you need to do is move the value of the next cell into the current cell and then decrement the size of the array by 1.
You have a couple options, you could use the RemoteAt Method of the List<T>, that's your simple option, which would work like this: - List<int> list = new List<int>(){ 1, 2, 3, 4, 5, 6 };
-
list.RemoveAt(1);
Would remove the first element in the array.
If, however, you're forced to use arrays you could write an extension method like this one: -
public static T[] RemoveAt<T>(this T[] source, int idx)
-
{
-
T[] destination = new T[source.Length - 1];
-
if( idx > 0 )
-
Array.Copy(source, 0, destination, 0, idx);
-
-
if( idx < source.Length - 1 )
-
Array.Copy(source, idx + 1, destination, idx, source.Length - idx - 1);
-
-
return destination;
-
}
-
Which could be used like above: -
int[] array = new int[4] {2, 4, 6, 8};
-
array.RemoveAt(1);
-
Hope that helps :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: ehames |
last post by:
Hi guys,
As far as I know, for any type T, and positive integer N, if I have an
array declared as:
T array;
then,
&array and array are the same element. Is there any reason why a
|
by: Tony |
last post by:
I have this code:
Dim elementi() As String = Split(book, "pages")
I have to do remove first item from array elementi ... any help? Thanks
|
by: Jared.Holsopple |
last post by:
Hi all,
I have a dynamically allocated array of doubles in VC++ .NET. When I
view the array in the watch window with "arrayName, 10", it displays
the correct value for arrayName, but arrayName...
|
by: brasilino |
last post by:
Hi Folks:
I've been looking (aka googling) around with no success.
I need a usability beyond 'pop()' method when removing an Array
elements. For example:
oName = new...
|
by: Arkady Frenkel |
last post by:
Hi, guys!
Is it possible to write byte array, I receive already from one stream to
another, but not from first element, without copy part of array to new one.
I mean I have byte array and need to...
|
by: John den Haan |
last post by:
Hello!
I was wondering how I can pass a pointer to a 2D-array (declared with a
malloc-construct) as if it were a continuous 1D-array of the size
(rows*cols)? Is this at all possible, and if so,...
|
by: Chris Forone |
last post by:
Hello Group,
there is some memberfunc for std::valarray to return a pointer to the
first element in the array. How do i use this? Thanx a lot.
HAND Chris
|
by: Szabolcs Borsanyi |
last post by:
I know that this topic has been discussed a lot, still I'd appreciate
a clear cut (and correct) answer:
I pass a multidimensional array to a function, which is defined as
int f(int a)
{
int...
|
by: CSharper |
last post by:
I am trying to use the following; I have an array with bunch of values
in it. I am trying to find a value that contains part of the string I
am passing
eg
string array = {"help","Csharp rocks"}
...
|
by: mab464 |
last post by:
I have this code on my WAMP server running on my XP machine
if ( isset( $_POST ) )
{
for($i=0; $i<count($_POST);$i++)
{
if ($ans != NULL )
$ans .= ", " . $_POST ; // Not the first...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |