0

So I'm trying to implement a method in a Grid class that returns the tile (which is an instance of the tile class) specified by two coordinates the user inputs. But I have no idea how to go about implementing it.

I have an ArrayList that contains all the instances of the tiles like this tiles.add(new Tile(x, y));

This is the method in question and my attempt at returning the desired tile:

   public ArrayList<Tile> getTileAtPos(int xCoor, int yCoor) {
        // if the coordinates are out of bounds
        // return null;

        // else
        return tiles.get(/*???*/); //   <---------------------- have no clue
    }
2
  • Not enough detail provided in the question, but it's likely to be either tiles.get(yCoor * width + xCoor) or tiles.get(xCoor * height + yCoor), depending on how you define your grid. Commented Nov 4, 2022 at 1:58
  • Shouldn't that return type be Tile rather than ArraylList<Tile>? Commented Nov 4, 2022 at 2:04

2 Answers 2

1

I recommend you reading on the documentation.

Anyway if you don't want to, the explanation is that the get() method for ArrayList uses the index to return the object.

ArrayList is not a Dictionary/Map based data structure where it has raw functions to compare based on object similarity.

The following when compared to arrays:

int_arr[2]; // some number

versus

int_arraylist.get(2); // some number

Are the exact same.

If you want to find an element based on ITS RAW PROPERTIES, in this case your xCoords and yCoords. Then you have to use a loop to check the individual elements in the ArrayList if they match the properties. Something like this:

if(xCoord > boundX || yCoord > boundY) {
    return null;
}
for(Tile e : tiles) {
    if(e.xCoord == xCoord && e.yCoord == yCoord) // change to match your accessor methods or however you access the x and y
        return e;
}
return null;

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

You wanna perform a search on the List and return the tile that matches the coordinates, you probably wanna change the Signature of the method to take in the List you wanna perform the search on and return the list of matches, and never return a null from a method that returns a collection, always return an empty collection instead.

public List<Tile> getTileAtPos(int xCoor, int yCoor, List<Tile> tiles) {

     return tiles.stream()
            .filter(t -> t.getXCoor() == xCoor && t.getYCoor() == yCoor)
            .collect(Collectors.toList());
}

@Getter
class Tile {
    private int xCoor;
    private int yCoor;
}

If only one Tile can match the condition of being at x and y then the method changes to:

public Tile getTileAtPos(int xCoor, int yCoor, List<Tile> tiles) {

    return tiles.stream()
            .filter(t -> t.getXCoor() == xCoor && t.getYCoor() == yCoor)
            .findFirst().orElse(null);

}

in this case it returns null if the tile is not found.

But I would prefere to have the method return an Optional instead, so the user of the method knows that he's not always guaranteed to have a result and mitigate the risk of nullpointer:

public Optional<Tile> getTileAtPos(int xCoor, int yCoor, List<Tile> tiles) {

    return tiles.stream()
            .filter(t -> t.getXCoor() == xCoor && t.getYCoor() == yCoor)
            .findFirst();

}

3 Comments

Interesting, but I feel like from this user posting this question that understanding this unintuitive idea of using the stream() method wouldn't be as fun. As loops are basic and easier to understand to a beginner.
Yeah I saw you already provided a pretty well explained answer of the imperative way, so didn't wanna make another one, this way he can have both options.
Ah. Well, there are so many and odd ways to just iterate over a Collection in java! Ha!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.