0

I am working with Arraylist of objects. while i already succeeded in delete values from Arraylist while they are numeral.

i'm having trouble delete from arraylist while they are String here is the example :

public class ArrayListDemo {

 public static void main(String[] args) {

    // create an empty array list with an initial capacity
    ArrayList<Namer> arrlist = new ArrayList<Namer>( );

    // use add() method to add values in the list
    arrlist.add(new Namer("1","A","G"));
    arrlist.add(new Namer("2","E","G"));
    arrlist.add(new Namer("3","F","G"));

    System.out.println("Size of list: " + arrlist.size());

    // let us print all the values available in list
    for (Namer value : arrlist) {
      System.out.println("age = " + value.age);
     }  

    arrlist.remove("3");

    System.out.println("Now, Size of list: " + arrlist.size());

     for (Namer value : arrlist) {
      System.out.println("age = " + value.age); //System.out.println("Value = " + value);
    }  
  }
}   

and the result of running it proof it doesnt deleted the spesific row i need to delete if with String and cant use number as "Key" .

Size of list: 3
age = 1
age = 2
age = 3

Now, Size of list: 3
age = 1
age = 2
age = 3

what can i make it to work with String in order to delete ?

if that's help

this is the object of arrayList

public class Namer
{
    // instance variables - replace the example below with your own
    public String age;
    public String name;

    public String L_name;


    public Namer(String a, String na , String Lname)
    {
        // initialise instance variables
         age =a;
         name=na;
         L_name=Lname;
   }

}

3 Answers 3

6

You have Namer objects in your ArrayList, not Strings. When you call remove("3"), the ArrayList will look for an object that returns true when equals is called on it with "3". Of course, no String will compare equals with any Namer.

You must do the comparison yourself with the name field, and remove the appropriate item. This can be done with an Iterator and its remove method.

Sign up to request clarification or add additional context in comments.

3 Comments

please tell me, am i suppsed to create new method called equals ? im my class Namer ? and where do i call this method equals ?
No, just call equals on the name attribute, a String.
i am sorry but could you be more specific please ?
2

You have to use the object or the index to remove and you are not using either ... Try this code :

arrlist.remove(2);

or

// create an empty array list with an initial capacity
ArrayList<Namer> arrlist = new ArrayList<Namer>( );

Namer namer1= new Namer("1","A","G");
Namer namer2= new Namer("2","E","G");
Namer namer3= new Namer("3","F","G");

// use add() method to add values in the list
arrlist.add(namer1);
arrlist.add(namer2);
arrlist.add(namer3);

arrlist.remove(namer3);

Comments

0

The method arrlist.remove(idx) takes as paramenter the (interger) index of the element to be removed from the array. There's no such thing as a "key" for ArrayLists.

You probably want to use java.util.Map<Integer,Namer>.

1 Comment

The OP is intending to use the overload that takes an Object parameter: public boolean remove(Object o)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.