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 toarray method in java?

562


What are disadvantages of java?

525


Explain a few methods of overloading best practices in java?

515


Can we convert list to set in java?

514


What are the ways to instantiate the class class?

603






what is the difference between yielding and sleeping? : Java thread

525


What is factor r?

529


How to sort double array in java?

593


How many bits is a boolean?

627


What is difference between printf and scanf?

523


Why pass by reference is not possible in java?

502


Can you tell me range of byte?

566


What about method local inner classes or local inner classes in java?

561


Addition to previous section relative word 5th one was Putrid ans: rotten, also there was prob. in 1st section on bucket weight ans:10kg, also there was a prob. on train speed to find bridge length ans:800 mtrs.

1615


What are identifiers in java?

578