Array push&pop

  • Thread starter Thread starter Aaron
  • Start date Start date
A

Aaron

I would like to move the values of an array up one in the index.
This is what i tried but it didn't work.


int[] intArr = new int[5];
Array.Copy(intArr, 0, intArr 1, 4);
intArr[0] = userinput;

//this is how it should work
userinput: 5
intArr {5,null,null,null,null}

userinput: 4
intArr {4,5,null,null,null}

userinput: 9090
intArr {9090,4,5,null,null}
////

Thanks in advance
 
Not sure what you mean, but the following program appears to work:

using System;

public class ShiftArray {
private static void Main() {
int[] foo = new int[5];
int count = 9;

while(count > 0) {
foo[0] = count--;
DumpContents(foo);

Array.Copy(foo, 0, foo, 1, 4);
}
}

private static void DumpContents(int[] array) {
for(int i = 0; i < array.Length; i++) {
Console.Write(array);
}
Console.WriteLine();
}
}
 
Aaron said:
//this is how it should work
userinput: 5
intArr {5,null,null,null,null}

userinput: 4
intArr {4,5,null,null,null}

userinput: 9090
intArr {9090,4,5,null,null}
////

Have a look at System.Collections.Stack.
 
Back
Top