2017-11-10 79 views
0

各位置で1から4までのランダムな値を持つ座標の2次元配列を作成しようとしています。私は現在、値の初期化に問題があります。ここでは方法のための私の現在のコードは次のとおりです。Javaで座標の2次元配列を作成する

public void createMap(){ 
    for (int i = 1; i < 20; i ++){ 
     for (int j = 1 ; j < 20; j ++) { 

      coord[i][j] = setCoordinates(random.nextInt(4) + 1, random.nextInt(4) + 1); 
     } 
    } 

    System.out.println(getCoord()); 
} 

と、この方法:

public Coordinates setCoordinates (int row, int column){ 
    this.row = row; 
    this.column = column; 
    return coord[row][column]; 
} 

と座標クラス:

public class Coordinates { 
int row; 
int column; 


public void setColumn(int column){ 
    this.column = column; 
} 

public void setRow(int row){ 
    this.row = row; 
} 

public int getRow(){ 
    return row; 
} 

public int getColumn(){ 
    return column; 
} 

} 

結果は常にコンソールでnullです。 配列内の値を実際に初期化するコードを変更するにはどうすればよいですか? 最終目標は、2Dゲームの座標グリッドを作成することです。戻り値の型のようなGUIのGridPaneでこれを使用しようとしている場合は、私が覚えておくべきことがありますか?より多くの情報が必要な場合はお知らせください。あなたの助けを前にありがとう。

例1、で:

+0

これは私がforループ内でやろうとしていることですが、明らかにそれは完全に間違っていますlolol @Caleb_McCreary –

+0

実際には、Coordinates [] [] coord = new Coordinates [20] [20];それが問題の原因になるのでしょうか? @Caleb_McCreary –

答えて

0
public Coordinates setCoordinates (int row, int column){ 
    Coordinates c = new Coordinates(); 
    c.setRow(row); 
    c.setColumn(column); 
    return c; 
} 

この場合COORDは、私はあなたの説明から多くを取得することはできませんが、私はあなたに座標クラスとない2つの完全な例を示していますCoordinates[][] coord = new Coordinates[x][y];

0

する必要がありますあなた座標オブジェクトを返すsetCoordinate機能:

package com.company; 

import java.io.Console; 
import java.util.concurrent.ThreadLocalRandom; 

public class Main { 

    public static void main(String[] args) { 
    // write your code here 

     Coordinates[] coord = new Coordinates[20]; 

     createMap(); 
    } 

    public static void createMap(){ 
     Coordinates[] coord = new Coordinates[20]; 

     for(int i = 0; i < 20; i ++){ 
      coord[i] = setCoordinates(ThreadLocalRandom.current().nextInt(0, 99) + 1, ThreadLocalRandom.current().nextInt(0, 99) + 1); 
     } 

     for(int i = 0; i < 20; i ++){ 
      coord[i].getCoord(); 
     } 
    } 

    public static Coordinates setCoordinates (int row, int column){ 
     Coordinates c = new Coordinates(); 
     c.setRow(row); 
     c.setColumn(column); 
     return c; 
    } 

    public static class Coordinates { 
     int row; 
     int column; 

     public Coordinates(){ 
      //constructor 
     } 

     public void setColumn(int column){ 
      this.column = column; 
     } 

     public void setRow(int row){ 
      this.row = row; 
     } 

     public int getRow(){ 
      return row; 
     } 

     public int getColumn(){ 
      return column; 
     } 

     public void getCoord(){ 
      //just return and print the coordinates 
      System.out.println("ROW: " + this.getRow() + "  COL: " + this.getColumn()); 
      //change void to return and return value if you like :) 
     } 

    } 
} 

をあなたはgetCoord(で遊ぶことができますしたい場合)との共同の配列を返します

public int[] getCoord() 
     { 
      int coords[] = new int[2]; 

      coords[0] = this.getRow(); 
      coords[1] = this.getColumn(); 

      return coords; 
     } 

及びその後座標

戻らず、この

for(int i = 0; i < 20; i ++){ 
      int coords[] = coord[i].getCoord(); 
      System.out.println(coords[0] + " - " + coords[1]); 
     } 

及び実施例2のように、これを印刷することができる:[0]行と[1]このようなカラムであることを知ることによって座標

package com.company; 

import java.io.Console; 
import java.util.concurrent.ThreadLocalRandom; 

public class Main { 

    public static void main(String[] args) { 
    // write your code here 

     Coordinates[] coord = new Coordinates[20]; 

     createMap(); 
    } 

    public static void createMap(){ 
     Coordinates[] coord = new Coordinates[20]; 

     for(int i = 0; i < 20; i ++){ 
      coord[i].setCoordinates(ThreadLocalRandom.current().nextInt(0, 99) + 1, ThreadLocalRandom.current().nextInt(0, 99) + 1); 
     } 

     for(int i = 0; i < 20; i ++){ 
      coord[i].getCoord(); 
     } 
    } 

    public static class Coordinates { 
     int row; 
     int column; 

     public Coordinates(){ 
      //constructor 
     } 

     public void setColumn(int column){ 
      this.column = column; 
     } 

     public void setRow(int row){ 
      this.row = row; 
     } 

     public int getRow(){ 
      return row; 
     } 

     public int getColumn(){ 
      return column; 
     } 

     public void setCoordinates (int row, int column){ 
      this.setRow(row); 
      this.setColumn(column); 
     } 

     public void getCoord(){ 
      //just return and print the coordinates 
      System.out.println("ROW: " + this.getRow() + "  COL: " + this.getColumn()); 
      //change void to return and return value if you like :) 
     } 

    } 
} 
関連する問題