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

  • Home
  • SEARCH
  • 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 8138437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:29:37+00:00 2026-06-06T11:29:37+00:00

I want to create a 2D array which can be used for two different

  • 0

I want to create a 2D array which can be used for two different purposes, in two different classes.
lets say i have a 2d array of int e.g.

int[][] grid = {{1, 1, 1, 1},
                {1, 1, 1, 1},
                {1, 1, 1, 1},
               {1, 1, 1, 1}}; 

my first purpose for this 2D array is to populate buffered images as part of a 2d game (the buffered image representing a layar of rock.

now lets say i wanted to use the exact same 2D array for define the data structure which would allow me create a search function for AI, basically using A* algorithm i want to be able to assign each position in the 2D array with a “cost” for moving.
My aim is to create a 2D java game that would be effecient and wont be re-inventing the wheel when it comes to trying to do something. My hope from my question is to be able to declare a 2D array in one class and use that same 2D array to do two things, allocate a number to each element in that array so i can calculate the costs of travelling and also use the 2D array to display an identical image on each element of that array.

the following is what i’ve done so far.

import java.util.*;

public class Map {

    boolean goal_Position = false;
    boolean start_Position = false;
    List neighbors = new ArrayList();

    public int createGrid(int x, int y) {
        int[][] grid = {{1, 1, 1, 1},
            {1, 1, 1, 1},
            {1, 1, 1, 1},
            {1, 1, 1, 1}};
        return grid[x][y];
    }

}

public class MainMap {

    public void soemthing(){
         Map m = new Map();
       for(int i = 0; i < createGrid[][].length; i++){
           for(int j = 0; j < createGrid[][].length; j++){

           }
       }
    }
    public static void main(String something[]){

        System.out.print(""+soemthing());

    }
}

i hope my question is not to vague or incoherent, the hope is to have an array that can be used for different things.I am aware that they code isn’t working the last few lines:

for(int i = 0; i < createGrid[][].length; i++){
           for(int j = 0; j < createGrid[][].length; j++){

           }
       }

throws a class expected error and i know the void doesn’t work when trying to return it in the main method. Assistance regarding this would be 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-06T11:29:39+00:00Added an answer on June 6, 2026 at 11:29 am

    Instead of returning a single element in the array, why don’t you
    return the whole array? Like this:

    public int[][] createGrid() {
        int[][] grid = {{1, 1, 1, 1},
            {1, 1, 1, 1},
            {1, 1, 1, 1},
            {1, 1, 1, 1}};
        return grid;
    }
    

    And then access it like this:

    Map mapInstance = new Map();
    int[][] grid = mapInstance.createGrid();
    for(int i = 0; i < grid.length; i++){
           for(int j = 0; j < grid[0].length; j++){
    
           }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create an array which has two columns. The output should contain
I want to create a two-dimensional array in which I want to store records
Consider I have an array of elements out of which I want to create
I want to create a component which can be used like: <mc:chart data=#{bean.data} width=200
I want to create a Python dictionary which holds values in a multidimensional array
Can I create a class in VB.NET which can be used from C# like
I want to create an array of arrays in PowerShell. $x = @( @(1,2,3),
I want to create an array holding a function f(x,y,z) . If it were
i want to create an array with 2 keys and i want them in
I want to create an array of numbers: 10, 9,8...to 1. But when I

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.