473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to remove first element from int array

1
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()

Expand|Select|Wrap|Line Numbers
  1.         public void addFirst(int value)
  2.         {
  3.             if (isFull())
  4.             {
  5.                 throw new Exception("List full");
  6.             }
  7.             else
  8.             {
  9.                 for (int pos = size;pos > 0; pos-- )
  10.                 {
  11.                     values[pos] = values[pos - 1];
  12.                 }             
  13.                 values[0] = value;
  14.                 size++;
  15.             }
  16.         }
Nov 14 '12 #1
2 14386
Rabbit
12,516 Expert Mod 8TB
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.
Nov 14 '12 #2
PsychoCoder
465 Expert Mod 256MB
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:


Expand|Select|Wrap|Line Numbers
  1. List<int> list = new List<int>(){ 1, 2, 3, 4, 5, 6 };
  2. 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:

Expand|Select|Wrap|Line Numbers
  1. public static T[] RemoveAt<T>(this T[] source, int idx)
  2. {
  3.     T[] destination = new T[source.Length - 1];
  4.     if( idx > 0 )
  5.         Array.Copy(source, 0, destination, 0, idx);
  6.  
  7.     if( idx < source.Length - 1 )
  8.         Array.Copy(source, idx + 1, destination, idx, source.Length - idx - 1);
  9.  
  10.     return destination;
  11. }
  12.  
Which could be used like above:

Expand|Select|Wrap|Line Numbers
  1. int[] array = new int[4] {2, 4, 6, 8};
  2. array.RemoveAt(1);
  3.  
Hope that helps :)
Nov 15 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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
3
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
8
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...
19
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...
3
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...
4
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,...
10
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
7
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...
6
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"} ...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.