title: How to find neighbors in a multi-dimensional array?
body: <p>Let's say we have a N-dimensional array A with the dimension N determined at runtime. </p>
  
  <p>I wonder whether there is any way I can find all neighboring elements in A of certain element A[a<sub>1</sub>][a<sub>2</sub>]...[a<sub>N</sub>] without invoking recursive methods.</p>
  
  <p>In a 2-dimensional case it is easy to write 8 neighboring elements of <code>A[i][j]</code>:
  <code>A[i-1][j-1], A[i-1][j], A[i-1][j+1], A[i][j-1], A[i][j+1], A[i+1][j-1], A[i+1][j], A[i+1][j+1]</code>, or code with a simple <code>for</code> loop. However, the same task on a higher-dimensional array does seem to need more tedious effort.</p>