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 / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do you execute a thread in java?

505


What is thread pool? How can we create thread pool in java?

622


What is an example of a keyword?

543


What is functional interface in javatpoint?

512


What is the difference between super class & sub class?

564






How to sort elements in a parallel array in java?

508


Is alive in java?

506


What is the escape character in java?

513


How are this() and super() used with constructors in java programming?

572


Can we use a switch statement with strings?

549


What are the advantages of passing this into a method instead of the current class object itself?

1171


Difference between == and .equals() ?

575


Why do we override tostring method in java?

506


What is starvation?

682


What is the use of generics? When was it added to the Java development Kit?

555