Note: This question is for my assignment. So there are some rules which might seem to be not normal.
I just realized my question before doesn't make sense. So I edited it.
Assume that I have a list containing integers [1,2,3,4,1,2,1,5]
By using an iterator, how can I remove duplicated items while keeping the order of items undisrupted?
The expected result for above list should be [1,2,3,4,5] And contain() method is not allowed to use.
What I currently came up with is the same as what Manash Ranjan Dakua answered in this question How do I remove repeated elements from ArrayList?
public static void main(String[] args){
ArrayList<Object> al = new ArrayList<Object>();
al.add("abc");
al.add('a');
al.add('b');
al.add('a');
al.add("abc");
al.add(10.3);
al.add('c');
al.add(10);
al.add("abc");
al.add(10);
System.out.println("Before Duplicate Remove:"+al);
for(int i=0;i<al.size();i++){
for(int j=i+1;j<al.size();j++){
if(al.get(i).equals(al.get(j))){
al.remove(j);
j--;
}
}
}
System.out.println("After Removing duplicate:"+al);
}
But if I wish to use Iterator instead of for loops here? How can I achieve that(remove duplicated items while keeping order unchanged)?
If the question is not clear enough or too vague. Pls point out! I'll try to rephrase it.
Thanks for help!