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 8780733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:07:52+00:00 2026-06-13T20:07:52+00:00

I am writing a program that deals with a 2D array, does calculations based

  • 0

I am writing a program that deals with a 2D array, does calculations based on surrounding elements and then creates a new array. To start I created a simple java program to test different cases. I want to correctly deal with if the element is on an edge of the array and if so set the "up, down, left, or right" equal to that element. My test program works for the element being on the top, left, or not on an edge, but not for the bottom or right. This is my current code:

public class ArrayTest 
{ 
   public static void buildE(int[][] array, int y, int x)
   {
      int up; 
      int down; 
      int left;
      int right; 

      //if element is on the top left
      if (y == 0 && x == 0)
      {
         up = array[y][x];
         down = array[y + 1][x];
         left = array[y][x];
         right = array[y][x + 1];
      }

      //if element is on bottom right
      else if (y == array.length && x == array.length)
      {
         up = array[y - 1][x];
         down = array[y][x];
         left = array[y][x - 1];
         right = array[y][x];
      }

      //if element is on top right
      else if(y == 0 && x == array.length)
      {
         up = array[y][x];
         down = array[y + 1][x];
         left = array[y][x - 1];
         right = array[y][x];
      }

      //if element is on bottom left
      else if (y == array.length && x == 0)
      {
         up = array[y - 1][x];
         down = array[y][x];
         left = array[y][x];
         right = array[y][x + 1];
      }

      //if element is on top 
      else if (y == 0) 
      { 
         up = array[y][x];
         down = array[y + 1][x];
         left = array[y][x - 1];
         right = array[y][x + 1];
      }  

      //if element is on left
      else if (x == 0)
      {
         up = array[y - 1][x];
         down = array[y + 1][x];
         left = array[y][x];
         right = array[y][x + 1];
      }

      //if element is on bottom
      else if(y == array.length)
      {
         up = array[y - 1][x];
         down = array[y][x];
         left = array[y][x - 1];
         right = array[y][x + 1];
      }

      //if element is on right
      else if (x == array.length)
      {
         up = array[y - 1][x];
         down = array[y + 1][x];
         left = array[y][x - 1];
         right = array[y][x];
      }

      //if element is not on an edge 
      else
      {
         up = array[y - 1][x];
         down = array[y + 1][x];
         left = array[y][x - 1];
         right = array[y][x + 1];   
      }

      System.out.println();
      System.out.print("#####################################");
      System.out.println();
      System.out.println("Array Element: " + array[y][x]);
      System.out.println("Up: " + up);
      System.out.println("Down: " + down);
      System.out.println("Left: " + left);
      System.out.println("Right: " + right);
   }

   public static void outputArray(int[][] array)
   {
      for(int row = 0; row < array.length; row ++)
      {
         for (int column = 0; column < array[row].length; column++)
            System.out.printf("%d ", array[row][column]);
         System.out.println();
      }
   }

   public static void main(String[] args)
   {
      int [][] myArray = {{1, 12, 13, 14, 15}, {2, 22, 23, 24, 25},
         {3, 32, 33, 34, 35}, {4, 42, 43, 44, 45}, {5, 52, 53, 54, 55}};
      outputArray(myArray);
      buildE(myArray, 4, 0);
   }
}

Furthermore if I set y = 4 it does not recognize this as myArray.length. However I do not think this will be an issue in my actual program if I iterate through until array.length. Any help would be much appreciated!

  • 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-13T20:07:54+00:00Added an answer on June 13, 2026 at 8:07 pm

    array indexes start from 0, if your array is of length 5, the last elemnt will be accessed at 4th index. in your code

      else if (y == array.length && x == array.length)
    {
    
        up = array[y - 1][x];//Exception here
        down = array[y][x];
        left = array[y][x - 1];
        right = array[y][x];
    
    }
    

    change it to:

     else if (y == array.length-1 && x == array.length-1)
        {
    
            up = array[y - 1][x];
            down = array[y][x];
            left = array[y][x - 1];
            right = array[y][x];
    
        }
    

    follow the same approach in other else if’s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program that reads huge file (3x280 GB) and does a fitting
I am writing a program that permutes a list of names based on a
I'm writing a program that lets the user input 6 temperature readings, and then
I am writing a program that downloads images to the hard drive and then
I'm writing a python program that deals with a fair amount of strings/files. My
I am writing a program that deals with cards and a hand. A hand
For homework, I am writing a program that deals with a lot of time_t
I am writing an Objective-C program that deals with low level image memory. I
Im writing a program that should read input via stdin, so I have the
I'm writing a program that handles DBs and writes any changes into ListView for

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.