How to declare unique ArrayList ?

Answer Posted / surya chandra mohan , visakhap

small modification for first program. But that one is really excellent.i really proud Indresh_ips.


import java.util.*;
class ArrayListUnique2 {

static public ArrayList uniqueArrayList(ArrayList arrl) {
int i;
String check="";
for(int k=0;k<arrl.size();k++) {
String s=arrl.get(k).toString();


if(!check.contains(s)){
check = check+s+", ";
}else{
arrl.remove(s);
}
}
return arrl;
}
public static void main (String[] args) {
ArrayList al= new ArrayList();

al.add("A");
al.add("A");
al.add("B");
al.add("C");
al.add("B");
al.add("A");
al.add("D");
al.add("D");
al.add("D");
al.add("D");
al.add("A");
al.add("B");
al.add("F");
al.add("G");
al.add("G");
al.add(10);
al.add(5);
al.add(10);
al.add(5);
System.out.println ("Before declaring unique:"+al);

ArrayList a=ArrayListUnique.uniqueArrayList(al);

System.out.println ("after declaring unique: "+a);
}
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why is it called a string?

573


What is the difference between exception and error in java?

504


How can we run a java program without making any object?

554


What is proper subset?

543


What is void class in java?

536






Explain the purpose of garbage collection in Java?

584


What is run time allocation?

585


What is the difference between class & object?

565


How many types of flags are there?

532


What is the collection interface?

599


What type of language is java?

554


How to perform bubble sort in java?

603


When do I need to use reflection feature in java?

631


What is the difference between a switch statement and an if statement?

525


Why synchronization is important?

601