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
Can you sort a list in java?
What does n mean?
What is a static class in java?
Does treeset use compareto?
When we serialize an object does the serialization mechanism saves its references too?
What is use of functional interface in java 8? Explain
What is replaceall in java?
when you will synchronize a piece of your code? : Java thread
What mechanism does java use for memory management?
Why do we use regex?
What is bigger kb or mb?
When a lot of changes are required in data, which one should be a preference to be used? String or stringbuffer?
What is a jagged array in java?
what do you understand by synchronization? : Java thread
What is private public protected in java?