Results 1 to 4 of 4
  1. #1
    cecily is offline Member
    Join Date
    Jul 2007
    Posts
    33
    Rep Power
    0

    Default how to remove an object from the array list

    I'm trying to remove a given object from the array list.
    But it doesn't work.
    what's the problem?
    Java Code:
    public boolean removeSomething(Student student)
    {
    student.remove(student);
    return true;
    }

  2. #2
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    I would guess that the student you are trying to remove is not found in the list so it is not reomving anything.
    The reasons for this could be:
    1) It isn't there
    2) The equals method for the student in the list and the one you are passing in do not match.
    Are you creating the student manually or retrieving it from the list prior to removing it?

    If that's not the case then I think you need to provide a little more information then we can give you a bit more help

    Regards :D

  3. #3
    cecily is offline Member
    Join Date
    Jul 2007
    Posts
    33
    Rep Power
    0

    Default

    This is my code

    Java Code:
    public boolean remove(Student student)
    {
    if(student==null)
    {
    return false;
    }
    else
    {
    students.remove(student);
    return true;
    }
    }

  4. #4
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    I was more after the code around what you have here.
    How are you obtaining the student instance which you are passing in to this method?
    Do you have a get method on this class which does :
    Java Code:
    students.get(n);
    somewhere in it? like:
    Java Code:
    public Student findWithName(String name) {
      Student student = null;
      Iterator<Student> iterator = students.iterator();
      while(null == student && iterator.hasNext()) {
        Student potential = iterator.next();
        if(potential.getName.equalsIgnoreCase(name)) {
          student = potential;
        }
      }
      return student;
    }
    If this is the case then passing the returned student into the remove function should work, however if you have created a new instance of Student then this may not work.
    Can you show me the code which calls remove and also the code for Student?

    Thanks.

Similar Threads

  1. How to convert List to Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:37 PM
  2. List views, a type of object
    By Leprechaun in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 04:07 AM
  3. Array List Problem
    By khamuruddeen in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 09:10 AM
  4. using an Array list
    By toad in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 10:08 PM
  5. Replies: 1
    Last Post: 08-01-2007, 09:55 AM

Posting Permissions

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