I want to add a certain Object in an ArrayList based on the user's input.
ie: If the user inputs two numbers I shall retrieve an Object of type building with given X, Y coordinates in a 2D Array I created
You mean something like this?
Building retrieveBuilding(Building[][] buildingMap) {
System.out.println("Enter X");
int x = scan.nextInt();
System.out.println("Enter Y");
int y = scan.nextInt();
return buildingMap[x][y];
}
ArrayList<Building> buildings = new ArrayList<Building>();
buildings.add(retrieveBuilding(buildingMap));
I assume 2D array buildingMap is already created and contains building on all coordinates - you need to add a check for null otherwise. You should also validate input to make sure that [X,Y] is in your 2D array.
By the way, I didn't test my code and I am not sure if it does exactly what you wanted but I hope it gave you some idea.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.