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?
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / anjan singh
ArrayList al=new ArrayList();
al.add("a");al.add("b");al.add(1);al.add
(2);al.add(1.1);al.add(2.1);
System.out.println("Before:\n");
System.out.println(al);
for(int i=0;i<al.size();)
{
if(al.get(i) instanceof String)
al.remove(i);
else
i++;
}
System.out.println("After:\n");
System.out.println(al);
Is This Answer Correct ? | 0 Yes | 0 No |
What do negative exponents mean?
When should you make a function static?
Write a function for palindrome and factorial and explain?
0 Answers Honeywell, Huawei, Zomato,
What is hash code collision?
What do you mean by scope of variable?
What is stack example?
Explain Global variables in Packages?
Why Static variable required in java?For ex,class A { static int a; int b; } Why static is required?
How does multithreading take place on a computer with a single cpu in java programming?
What are latest features introduced with java 8?
Which Math method is used to calculate the absolute value of a number?
What are checked exceptions?