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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:48:34+00:00 2026-06-06T18:48:34+00:00

i am having difficulty with 2D arrays and inserting objects that take 3 parameters

  • 0

i am having difficulty with 2D arrays and inserting objects that take 3 parameters (int x, int y, int cost)
This is the beginning of a search algorithm and admittedly im off to a very poor start. I will paste the code below. I am receiving a compiling error when i try to run this code and i’m very sure it is simple but i cannot resolve it.
The Map2 class i intend to use to implement the main bulk of the algorithms, such as sorting etc.

 import java.util.Arrays;


    public class Map2 {

        public static void main (String args[]){

         Points[][] grid = new Points[4][4];

         for(int i = 0; i < grid.length; i++){
             for(int j = 0; j < grid.length; j++){
                 grid[i][j] = new Points(i,j,1);
             }


         }
         System.out.print(Arrays.deepToString(grid));

    }
    }

This class is my object, it contains the movement cost from moving from one position to the next (the next step obviously would be to determine neighbors) and yes, this is part of trying to create a working A star algorithm.

 public class Points {
     int x;
     int y;
     int movement_cost;

    public  Points(int iX, int iY, int cost){
        x = iX;
       y = iY;
        movement_cost = cost;

    }

    public int getX(){
        return x;
    }
    public int getY(){
        return y;
    }
    public int getMovementCost(){
        return movement_cost;
    }
    public void setX(int x){
        this.x = x;
    }
    public void setY(int y){
        this.y = y;
    }
    public void setMovementCost(int cost){
        this.movement_cost = cost;
    }
    public String toString(){
        return ""+getX()+ ""+getY()+""+getMovementCost();
    }
}

This is the console read out after compiling (3 address spaces in memory)

    run:[[001, 011, 021, 031], [101, 111, 121, 131], 
[201, 211, 221, 231], [301, 311, 321, 331]]
BUILD SUCCESSFUL (total time: 2 seconds)

My hope here is simple, each object in the array will contain a reference of its coordinates in memory and contain a cost of movement, which would later be used to compare in order to determine the next best position (i will later implement things such as goal, start)

my question is: whats wrong with the code the way it is ?

I want to thank, in advance whomever responds as your responce will always be appreciated

CURRENT REVISION OF MY QUESTION V0.1:
Wow well thanks for the quick responces, i have learned something new today ^^ that Arrays.deepToString(grid)); is an amasing tool i was not aware of, however i am still receiving a runtime error . Thank you once again for your replies and once again for any further responces :). The code above has been revised as recommended, but the runtime error still exists

  • 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-06T18:48:37+00:00Added an answer on June 6, 2026 at 6:48 pm

    There are several things that are wrong with your code:

    • Your nested loops assume that the array is square (you iterate both dimensions to grid.length),
    • You print the entire array after initializing each row, and
    • You print the array incorrectly (Java array do not print their content when passed to System.out.println)

    The first item is OK if your matrix is indeed always square. The second item is easy to fix by moving the output outside of the second nested loop.

    The third item is the hardest. It would be a good exercise to write a static method that takes your 2D array, and prints it out element-by-element with two nested loops. You can also use System.out.println(Arrays.deepToString(grid)); if you would rather use a system function.

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

Sidebar

Related Questions

I'm having some difficulty passing values and arrays between this two functions Here's the
I am trying new things with arrays and having some difficulty. I am trying
I am having difficulty creating sessions for my mobile application that is written in
I have a sum that I'm trying to compute, and I'm having difficulty parallelizing
I'm having some difficulty with an array that is (outside of an asynchronous call)
I am having difficulty in this conversion. I don't know if there is a
I am having difficulty to explain this problem, but I will try anyway. I
I'm having trouble with multidimensional arrays in PHP. I'm assuming this should be simple
I am having difficultly with syntax and structure of JSON objects/arrays. { accounting :
I am having difficulty passing an array as an argument into int main() with

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.