Delete Element from Array Using For Loop + If Statement - C# -


i have array, temparray[] = {1,3,-1,5,7,-1,4,10,9,-1}

i want remove every single -1 array , copy remaining arrays new array called original, should output numbers 1,3,5,7,4,10,9

i can use if statement within loop!

this have far, keep getting error message, system.indexoutofrangeexception

    (int = 0; < temparray.length; i++)         {             if (temparray[i] != -1)             {                 //error occurs @ line                 //my attempt set new array, original[i] equal temparray[i] values not -1.                 temparray[i] = original[i];             }         } 

if can use if statement in loop. looks school project. first count how many non negative numbers there in array. create new array length , fill array.

int[] temparray = new int[] {1,3,-1,5,7,-1,4,10,9,-1}; int[] original ;  int countnonnegative=0;  (int = 0; < temparray.length; i++) {             if (temparray[i] != -1)             {                 countnonnegative++;             }  }     original = new int[countnonnegative];     int index=0;     (int = 0; < temparray.length; i++)     {             if (temparray[i] != -1)             {                 original[index] = temparray[i];                 index++;             }     }     console.writeline("original length = "+original.length); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -