Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9139291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:22:06+00:00 2026-06-17T09:22:06+00:00

I have a 3D list (3 nested lists), and for each item in the

  • 0

I have a 3D list (3 nested lists), and for each item in the list, I need to find the adjacent items. Currently, I’m creating an array for each adjacent item, and storing that in each item. This is pretty resource intensive, so I’m looking to simplify it a bit.

I was considering using this approach:

for (int i = 0; i < 8; i++)
{
    switch (i)
    {
        case (0):
            mapCell neighbor = myMap.Depth[z].Rows[x].Columns[y - 1];
        case (1):
            mapCell neighbor = myMap.Depth[z].Rows[x].Columns[y + 1];
        case (2):
            mapCell neighbor = myMap.Depth[z].Rows[x - 1].Columns[y];
        case (3):
            mapCell neighbor = myMap.Depth[z].Rows[x + 1].Columns[y];
        case (4):
            mapCell neighbor = myMap.Depth[z].Rows[x + 1].Columns[y + 1];
        case (5):
            mapCell neighbor = myMap.Depth[z].Rows[x + 1].Columns[y - 1];
        case (6):
            mapCell neighbor = myMap.Depth[z].Rows[x - 1].Columns[y + 1];
        case (7):
            mapCell neighbor = myMap.Depth[z].Rows[x - 1].Columns[y - 1];
    }

    // do some stuff with the neighbor here
}

This is a lot better than what I have now in terms of performance, but I’m wondering if there is an easier way to accomplish this? It seems a bit messy, and I really feel like there would be a way to do this in 1 line, but I just cant figure out the math.

edit: Sorry, I may have omitted some important details. The code which works with the “neighbor” variable has been left out, as its very long and doesnt help with the solution. I do not need to maintain a list of the “neighbor” variables (Thats what I am currently doing, and it uses far too much memory (around 400megs). I just need a quick way to look at each item, find each of the adjacent 8 items, do something with them one at a time, and then move onto the next node. The above code will work to go through them, but it doesnt feel like the most optimized way to accomplish this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:22:07+00:00Added an answer on June 17, 2026 at 9:22 am

    It looks like you are actually finding neighbours in the 2D array at a given depth in your 3D data structure. I.e. you are ignoring neigbours at adjacent depths.

    Your approach is probably pretty performant. You could find some fancier way that involves less typing, e.g. this answer to a similar question on SO, but I doubt it will be faster.

    Obviously you also need to include some checks in your code to handle items at the edge which won’t have 8 neighbours.

    private void DoSomethingWithNeighbours(int x, int y, int z)
    {
        foreach (var neighbout in this.GetNeighbours(x, y, z)
        {
            // ...
        }
    }
    
    private IEnumerable<Item> GetNeighbours(int x, int y, int z)
    {
        if (x > 0)
        {
            if (y > 0)
            {
                yield return myMap.Depth[z].Rows[x - 1].Columns[y - 1];
            }
    
            yield return myMap.Depth[z].Rows[x - 1].Columns[y];
    
            if (y < ColumnCount - 1)
            {
                yield return myMap.Depth[z].Rows[x - 1].Columns[y + 1];
            }
        }
    
        if (y > 0)
        {
            yield return myMap.Depth[z].Rows[x].Columns[y - 1];
        }
    
        if (y < ColumnCount - 1)
        {
            yield return myMap.Depth[z].Rows[x].Columns[y + 1];
        }
    
        if (x < RowCount - 1)
        {
            if (y > 0)
            {
                yield return myMap.Depth[z].Rows[x + 1].Columns[y - 1];
            }
    
            yield return myMap.Depth[z].Rows[x + 1].Columns[y];
    
            if (y < ColumnCount - 1)
            {
                yield return myMap.Depth[z].Rows[x + 1].Columns[y + 1];
            }
        }
    }
    

    Or the following alternative which is rather more concise, with only a marginal performance cost:

    private IEnumerable<int> GetNeighbours2(int x, int y, int z)
    {
        for (int i = x - 1; i <= x + 1; ++i)
        {
            for (int j = y - 1; j <= y + 1; ++j)
            {
                if (i >= 0 && i < Rows && j >= 0 && j < Cols && !(i == x && j == y))
                {
                    yield return myMap.Depth[z].Rows[i].Columns[j];
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested list comprising ~30,000 sub-lists, each with three entries, e.g., nested_list
I have a tabpanel with items of nestedlists. Each nested list goes several levels
I have a nested list of data. Its length is 132 and each item
I have a JSON/Python nested list which represents a tree, each item being a
I have a set of nested unordered lists that represents page navigation. The list
I have to calculate the average of each column in a specific nested list
I have Python nested list that I'm trying to organize and eventually count number
How would I create a nested list, I currently have this public function getNav($cat,$subcat){
I am trying to remove items from a nested list in Python. I have
I have a large nested list that I am trying to animate using jquery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.