简体   繁体   English

如何使用java.awt中的Point类找到二维数组的点

[英]How to find the point of a two dimensional array using the Point class in java.awt

I have a two dimensional character array in a Map class 我在Map类中有一个二维字符数组

public class Map {
    private char[][] map;
}

which is 5 rows and columns in size (defined in a constructor). 大小为5行和列(在构造函数中定义)。 I also have a method in the same class 我在同一个班上也有一个方法

public Point findStart(){
   Point location=new Point();
      for ( int i=0; i<map.length; i++){
          if(map[i][i]=='s')
      System.out.println( "The starting position is in " + location.x + location.y );
      }
        return location;
    }

which iterates through the array to find the "s" character. 它遍历数组以找到"s"字符。 Once it locates the character, I wish to make use of java.awt 's Point class to find the location of where the char 's' is located. 一旦找到字符,我希望利用java.awtPoint类找到char所在位置的位置。 How can I get the point to print out? 我该如何打印出来?

As MaxG has pointed out, my code is missing a for loop. 正如MaxG指出的那样,我的代码缺少for循环。 The new method is 新方法是

public Point findStart(){

        Point location=new Point();

        for ( int i=0; i<map.length; i++ ){
          for( int j=0; j<map[i].length; j++ )
           if(map[i][j]=='s')               
            location.getLocation();

      }

      System.out.println(location.x+","+location.y);
      return location;
    }

I still get 0,0 as coordinates though. 我仍然得到0,0作为坐标。

UPDATE: I think Christopher Chamberlain brings up a valid point. 更新:我认为克里斯托弗·张伯伦提出了一个正确的观点。 In the project I am reading from a .txt file and placing each character into the array. 在项目中,我正在读取.txt文件,并将每个字符放入数组中。

//loads maps according to an integer parameter, which depends on what map the player is in (Map1, Map2, Map3, etc.)
    public void loadMap(int MapNum){

    //attemps to load .txt file
        try{
        String s="Map"+MapNum+".txt";
        File file=new File(s);
        Scanner sc=new Scanner(file);       

        while(sc.hasNextLine())
        {          
            for( int i=0; i<5; i++){

                char currentCharacter=sc.next().charAt(0);
                map[i][i]=currentCharacter;                           
            }  
        }      
        }
        catch(IOException exception){
            System.out.println(exception);
        }
    }

this method exists in the same class. 此方法存在于同一类中。 I could be messing up somewhere while reading each character but I cannot figure out where? 在阅读每个字符时,我可能会乱七八糟,但我不知道在哪里?

Are you looking for somehting like this ? 您是否正在寻找这样的东西?

public Point findStart() {
    Point location = new Point();
    for (int i = 0; i < map.length; i++) {
        for (int j = 0; j < map[i].length; j++) {
            if (map[i][j] == 's') {
                location.setLocation(i, j);
                break;
            }
        }
    }
    System.out.println(location.x + "," + location.y);
    return location;
}

I don't have enough reputation to comment... sorry... 我没有足够的声誉来发表评论...对不起...

Jaydip's solution looks like the way to do it. Jaydip的解决方案看起来就像是这样做的方法。 The only reason it wouldn't work is if map[i][j] == 's' never returns true . 它不起作用的唯一原因是map[i][j] == 's'从不返回true Are you sure map contains an 's' ? 您确定map包含's'吗? I think the problem has to do with how you fill map . 我认为问题与您如何填写map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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