Home > Software design >  How do I return a specific object from an ArrayList?
How do I return a specific object from an ArrayList?

Time:11-04

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
    }

CodePudding user response:

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!

CodePudding user response:

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();

}
  • Related