Results 1 to 2 of 2
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default checking 2D arrays for an object && checking arraylists for something

    So I have a new question. I am going to post it in this thread. I dont want to violate forum rules.

    so heres what I have:


    Question. Heres what i have:

    -1 2D array called landscape
    -1 arraylist called plants
    -1 arraylist called fish


    how do I check the location? of a 2d array and make sure it equals ROCK?
    and how do I check if there is another plant in the arraylist?

    Java Code:
    //My attempt
    
    /** Attempts to add the plant p to plant list, if possible.
    	 * <p>
    	 * First checks if the landscape in the plant's location is equal to ROCK.  If it 
    	 * is, then does not add the plant to the list.  Instead throws an 
    	 * IllegalPlantPositionException, passing 
    	 * IllegalPlantPositionException.PLANT_OVER_ROCK to the constructor.
    	 * <p>
    	 * Now checks for another plant (distinct from the parameter) that is in the 
    	 * same location as the parameter.  If one is found, then does not add the plant
    	 * to the list.  Instead throws an IllegalPlantPositionException,
    	 * passing IllegalPlantPositionException.TWO_PLANTS_IN_ONE_PLACE to the 
    	 * constructor.
    	 * <p>
    	 * Otherwise, adds the plant to the list "plants".
    	 */
    	public void addPlant(Plant p) {
    		
                    int col = p.getCol();
                    int row = p.getRow();
    		if (landscape[row][col] == ROCK) {
    			throw new IllegalPlantPositionException(IllegalPlantPositionException.PLANT_OVER_ROCK);
    		}
    		else if(landscape[row][col] ==     /*confused as to what to put 
    
                               to check if another fish is already in this position*/         ){
    		throw new IllegalPlantPositionException (IllegalPlant
                    PositionException.TWO_PLANTS_IN_ONE_PLACE);	
    			
    		}
    
    		else{
                    plants.add(p);
                  
    	}
    Thanks for help.
    Last edited by NoobieCode; 05-07-2013 at 05:19 AM.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: checking 2D arrays for an object && checking arraylists for something

    you can do

    2dArray [row][column] == rock. replace 2dArray with landscape and row && column with the index you want to check

    what do you mean by another plant? check that there isnt 2 the same plants? or just check that the arrayList has another plant to evaluate?

Similar Threads

  1. Checking if a Boolean Within Another Class Object is True
    By Alpharius120 in forum New To Java
    Replies: 5
    Last Post: 12-07-2012, 03:28 AM
  2. Compare two Arrays, checking for duplication
    By Redefine12 in forum New To Java
    Replies: 4
    Last Post: 02-03-2012, 09:57 PM
  3. Replies: 11
    Last Post: 11-02-2011, 06:38 PM
  4. Checking class of object
    By ianyappy in forum New To Java
    Replies: 2
    Last Post: 12-06-2010, 12:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •