java - Why are variable not recognised from inside for-statements? -


what trying create array pulls numbers array. i'm not sure if have gone right way. i've ways of returning statements functions/methods , can't find anything, not sure if possible.

anyway, issue having here 'return evenarray' below 'cannot find symbol.' not sure means?

public static int[] getevenarray(int[] array) {             int dividedby = 2;     int evenelement;     int evencount = 0;      for(int = 0; < array.length; i++)     {         int[] evenarray;         evenelement = array[i] % dividedby;          if(evenelement == 0)         {             evencount++;         }         else         {             array[i] = 0;         }          evenarray = new int[evencount];          for(int x = 0; x < evenarray.length; x++)         {             if(array[i] != 0)             {                 evenarray[x] = array[i];             }         }     }      return evenarray; } 

this tutorial 1 of lectures, it's little bit challenging least :-)0

evenarray defined within scope of for loop. (actually little worse that; you're redeclaring on each iteration discarding previous contents).

so once you're outside for loop can't refer it.

quickest fix use std::vector<int> type, , declare @ start of function. change return type of function same. don't forget size vector appropriately.

(moving on, smart lecturer ask returning std::vector potentially take deep copy of vector. pre c++11 you'd mention return value optimisation, can talk r-value references. no deep copy taken since move constructor used).


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -