How to declare unique ArrayList ?

Answer Posted / sudhakar

I have modified the above program. And made esay the
process.


import java.util.ArrayList;

public class UniqueArrayListValues {

static ArrayList assignValues() {
ArrayList a1 = new ArrayList();
a1.add("A");
a1.add("B");
a1.add("C");
a1.add("27");
a1.add("A");
a1.add("B");
a1.add("C");
a1.add("27");
a1.add("A");
a1.add("B");
a1.add("C");
a1.add("27");
return a1;
}

static ArrayList applyUniqueKey(ArrayList a1) {
ArrayList a2 = new ArrayList();
for (int i = 0; i < a1.size(); i++) {
if (!a2.contains(a1.get(i))) {
a2.add(a1.get(i));
}
}
return a2;
}

public static void main(String args[]) {
ArrayList beforeUniqueKey = assignValues();
System.out.println("Before applying Unique
Key:" + beforeUniqueKey);
ArrayList afterUniqueKey = applyUniqueKey
(beforeUniqueKey);
System.out.println("Afrer applying Unique
Key:" + afterUniqueKey);
}

}

Is This Answer Correct ?    5 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is empty list in java?

818


What are the restrictions imposed by a Security Manager on Applets?.

2290


What are the different conditional statements?

743


What is callable java?

736


What is the purpose of void?

732


Why is java called the platform independent programming language?

776


What does s mean in regex?

738


Why java is secure? Explain.

765


Why can't we use static class instead of singleton?

696


Name and explain the types of ways which are used to pass arguments in any function in java.

807


What is int short for?

714


Can we call the run() method instead of start()?

779


Why vector is used in java?

755


What is method in java ?

826


What is overloading and overriding in java?

902