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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:12:38+00:00 2026-05-28T14:12:38+00:00

Iam trying to create a java program in which, i will be able to

  • 0

Iam trying to create a java program in which, i will be able to change the position of value 1 to specific positions into an array.For example i have the value 1 in the position [5][4] and i want to move it to the next (or the previews position) and delete the value 1 and store the value 0 into the previous position. My problem is that i can move the value 1 but i can’t delete it from the previous position(store to the previous position the value 0).My code is the following:

for (int row = 0; row < 6; row++) {
    for (int col = 0; col < 6; col++) {
        int randomPosition = r.nextInt(2);

        array[row][col] = 0;
        array[2][3] = 1;
        array[4][4] = 1;

        if (array[row][col] == 1) {
            array[row][col] = 0;

            if (randomPos == 0) {
                array[row - 1][col] = 1;

            } else if (randomPos == 1) {
                array[row][col - 1] = 1;

            } else if (randomPos == 2) {
                array[row + 1][col + 1] = 1;

            }
        }
    }
}

for (int row = 0; row < cells.length; row++) {
    for (int col = 0; col < cells[row].length; col++) {
        System.out.print("  " + cells[row][col]);
    }
}
  • 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-05-28T14:12:39+00:00Added an answer on May 28, 2026 at 2:12 pm

    You’re probably getting an ArrayIndexOutOfBoundsException. This is because you’re referencing [row-1] and [col-1] (and +1 as well), but row or col might be 0 or the maximum index, and adding or subtracting 1 in those cases will make you try to reference an index that doesn’t exist.

    You’ll have to put in checks to make sure that the index you’re referencing actually exists.

    Example:

    if(randomPos==0){
        if(row-1 > 0)
            array[row-1][col] = 1;
    }
    

    Also, as per the comments, you should tell us what you’re actually seeing, and how you know it’s not working correctly. If there are any error messages that you can’t make sense of (try reading them first, of course!), then post those as well.

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

Sidebar

Related Questions

I am trying to create a java program that will take in a directory
I am trying to build a simple java program which creates a db file,
Am trying to implement a Java program which sends an Apple Push Notification to
I am trying to figure out how to create a java program that can
I am trying to create a simple simulation program of SIR-epidemics model in java.
I am trying to create a MonkeyRunner program in Java using Eclipse but :
I am trying to create a Key Listener in java however when I try
I am using Java 6 and am trying to create an HttpsURLConnection against a
I am trying to create a directory in Java. I think I have provided
I am trying to create my first GUI application using (Java + Eclipse +

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.