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

What are keywords in programming?

568


In a container there are 5 components. I want to display the all the components names, how will you do that one?

606


What are features of java?

561


What does the append?

497


how to handle exceptions in ejb?

1877






How does queue work in java?

532


How to check if a list is sorted in java?

590


What is bifunction in java?

680


What are the types of collections in java?

541


Why do we override tostring method in java?

513


Explain notifyall() method of object class ?

669


Why we used break and continue statement in java?

563


What are singleton services?

493


What is an example of declaration?

526


Can we assign the reference to this variable?

548