Results 1 to 4 of 4

Thread: ArrayList?

  1. #1
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default ArrayList?

    Ok So I have a project for school and i'm stuck on arraylist. I have a abstract class called superhero that has getSuitColor, setCape, isCaped(boolean) and setSuitColor. I've already made objects out of my classes and made and ArrayList out of them. But Now I'm trying to remove the superhero with capes.


    for ( int i = 0; i < heros.size(); i++)
    {
    if(heros.isCape() == true)
    {
    heros.remove(i);
    }//end if
    }//end for

    This is way I have but its not working cause it says "cannot find symbol - method isCape()"
    I've been siting here for 40 minutes trying to figure it out but I can't :[
    Any ideas?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: ArrayList?

    The ArrayList 'heros' does not have an isCaped() method. The various superhero objects in that List do.
    So you want to use the get() method to return the superhero at index 'i' and then call the isCaped() method on that.

    Note:
    Java Code:
    if (heros[i].isCaped() == true)
    you don't need the '== true' part.
    An 'if' statement simply requires a boolean to work with, which is what is returned by isCaped(), so no need to compare it with 'true'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: ArrayList?

    If heros is an ArrayList, Should that be:
    Java Code:
      if (heros.get(i).isCaped())
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: ArrayList?

    Doh!
    I actually meant to copy the code in the OP, but mucked up...doubly so!
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 10-11-2013, 06:18 PM
  2. Replies: 0
    Last Post: 09-21-2013, 12:28 AM
  3. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  4. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 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
  •