Results 1 to 5 of 5
  1. #1
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default ArrayList question

    I need to write a method that overwrites ArrayList's add method (the class I'm writing in is an extension of ArrayList) so that it takes in one argument and adds that to the ArrayList, but will not let the user add anymore arguments to the ArrayList, one is the max. It just returns false if the user tries to add anymore arguments. Can someone help me write this..I'm not sure where to begin..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Is this a state question? Can your add method detect a state and return false?
    The state being that an element has been added already.
    tries to add anymore arguments
    This bit is confusing. Arguments are what you put in () to pass to a method. You won't be overriding the add() method if you have different arguments than the base class has.
    Do you mean "add more elements"?

  3. #3
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Is this a state question? Can your add method detect a state and return false?
    The state being that an element has been added already.

    This bit is confusing. Arguments are what you put in () to pass to a method. You won't be overriding the add() method if you have different arguments than the base class has.
    Do you mean "add more elements"?
    Well here's what I wrote. Is this make sense?

    NOTE: The ArrayList is made of Objects called LatLon, the name of this class is PinPlacemark which extends Placemark, but Placemark extends ArrayList<LatLon>.

    Java Code:
    public class PinPlacemark extends Placemark {
    	
    	public boolean add(LatLon add){
    		if(this.size() == 0){
    			this.add(add);
    			return true;
    		}
    		if(this.size()>0){
    			return false;
    		}
    		else{
    			return false;
    		}
    	}
    
    }
    So, what I want this code to do is add the LatLon add instance, but not allow the user to add anymore LatLons. So, one is the maximum number of LatLons this ArrayList can hold.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Looks reasonable. What happens with it?

    Why are you using an ArrayList to hold only one element???? Makes no sense!!!

  5. #5
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Looks reasonable. What happens with it?

    Why are you using an ArrayList to hold only one element???? Makes no sense!!!
    Yeah idk about that yet, I'm still working on the assignment :), but that code does what I want it to?

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. Beginner question about ArrayList
    By kesi in forum New To Java
    Replies: 3
    Last Post: 09-20-2009, 12:30 AM
  3. arraylist question
    By lisa.lipsky in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-16-2009, 12:07 PM
  4. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  5. Help ArrayList.add()
    By eNine in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 02:13 PM

Posting Permissions

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