Q) I have a ArrayList object, in that object i have added 5
integer values, 5 float values, 5 string values. Now
question is how can delete particular type of data ( i.e all
int values or all float values or string values) in that
list object at a time?

Answer Posted / srivatsava

import java.util.*;
public class ArrayListDeleteParticularDataTypeExample {

/**
* @param args
*/
public static void main(String[] args) {
ArrayListDeleteParticularDataTypeExample obj = new ArrayListDeleteParticularDataTypeExample();
ArrayList al = new ArrayList();
al.add(new Integer(1));
al.add(new Integer(2));
al.add(new Integer(3));
al.add("A");
al.add("B");
al.add("C");
al.add(new Float(10.10));
al.add(new Float(20.20));
al.add(new Float(30.30));
System.out.println("ArrayList Size Before - "+al.size());
System.out.println("ArrayList Values Before- "+al);
Iterator it = al.iterator();
while(it.hasNext()){

if(it.next() instanceof String){
it.remove();
}
}
System.out.println("ArrayList Size After - "+al.size());
System.out.println("ArrayList Values After - "+al);
}

}

Is This Answer Correct ?    8 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is function and its uses?

569


Difference between throw and throws?

620


What is the difference between a method and a procedure?

549


When do we need to use internal iteration? When do we need to use external iteration?

607


What is mnemonic in assembly language?

553






What do you mean by global variable?

527


What are the disadvantages of object oriented programming?

599


What is meant by 'Class access modifiers'?

554


What is a class in java?

580


Can we override static methods in java?

591


Can you explain the usages of class.forname()?

609


Can an integer be null java?

555


Why Set interface contains unique elements, what internally implemented for this so that it contains unique elements?

7419


What is boolean strategy?

628


When does an object becomes eligible for garbage collection in java?

577