简体   繁体   中英

Passing a 2d Array into another class : Java

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 eg

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.

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++){

       }
   }
import java.util.ArrayList;

/**
 * Created by Stefan Ronnkvist on 8/5/2017.
 */

public class SplitStringToArray {

    public String[][] SplitStringToArray(String passedValue) {
        ArrayList<String> fundNames = new ArrayList<String>();
        ArrayList<String> fundValues = new ArrayList<String>();
        ArrayList<String> fundDates = new ArrayList<String>();

        String[] splitString = passedValue.split(",");
        for (String value : splitString) {
            if (value.matches(".*[a-zA-Z]+.*")) {
                fundNames.add(value.trim());
            } else if (value.indexOf("-") >= 0) {
                fundDates.add(value.trim());
            } else if (value.indexOf(".") >= 0) {
                fundValues.add(value.trim());
            } else {
                fundValues.add("0.00000");
            }
        }
        String[] fundDatesArray = new String[fundDates.size()];
        fundDatesArray = fundDates.toArray(fundDatesArray);

        String[] fundNamesArray = new String[fundNames.size()];
        fundNamesArray = fundNames.toArray(fundNamesArray);

        String[] fundValuesArray = new String[fundValues.size()];
        fundValuesArray = fundValues.toArray(fundValuesArray);

        String[][] returnArray = {fundNamesArray, fundDatesArray, fundValuesArray};
        return returnArray;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM