Hi there,

Say I have a 2D array, of which we are using as a gameboard for instance.

For example, a 5x5 2D array of the type char, and initialized with dots and a 'S'.

char[][] array = new char[5][5]

.....

.....

..S..

.....

.....

How can I find the indexes of the 2D array that are closest to the letter S (aka wrap around it), in terms of proximity on the board, and then turn this into a new array?

So in this example, the 8 dots that surround S can now be initialized into a new array.

**What I have tried:**
I have tried finding the Euclidean distance between a given point and the array elements that surround it, and then attaching the objects of that array to a length by means of a hashmap. Note that the above question is more for simplicity sakes, and my actual project works with custom-type 2D arrays, evident in the code below. Nevertheless, the concept is still applicable.

public static void getIndexes(Grid_23722002 grid, Piece_23722002 piece){
int row = piece.getRow();
int col = piece.getCol();
Piece_23722002[][] nhood = new Piece_23722002[8][8];
Map<Piece_23722002, Integer> unsortedLengthMap = new LinkedHashMap<Piece_23722002, Integer>();
for (int i =0; i < grid.getArrayForm().length; i++){
for (int j =0; j < grid.getArrayForm()[i].length; j++){
unsortedLengthMap.put(piece, grid.getArrayForm()[i][j].getLengthToPoint(grid, i, j, piece));
}
}
Map<Piece_23722002, Integer> sortedLengthMap = new LinkedHashMap<Piece_23722002, Integer>();
unsortedLengthMap.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.forEachOrdered(x -> sortedLengthMap.put(x.getKey(), x.getValue()));
}
public int getLengthToPoint(Grid_23722002 grid, int i, int j, Piece_23722002 piece){
int piecesRow = piece.getRow();
int piecesCol = piece.getCol();
int length = (int) Math.sqrt(Math.pow(grid.getArrayForm()[i][j].getRow() - piecesRow, 2) + Math.pow(grid.getArrayForm()[i][j].getCol() - piecesCol, 2));
return length;
}