• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

How to make a loop with List Iterator...

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have one Vector with result and I want to compare the first Element of the List with the next, and if the next is equal to first the next must be remove, but i dont know how?
Please help me!
Vector ligne = (Vector)result.elementAt(row);
List list= new ArrayList();
list.add(ligne.firstElement());
for (ListIterator i = list.listIterator(); i.hasNext(); ) {
String temp= (String)i.next();
String index= (String)i.previous();
if (index==null ? i.next()==null : index.equals(i.next())) {
i.remove();
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krassi,
The code below should work using iterators.

Regards,
Manfred.
 
Krassi Bogdanova
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Manfred,
the code remove all elements and I want display all elements without duplicates.
This Vector is the result of one Select and I dont know how many elemnts I will have and after the test if there exists duplicates I must print the result + the result of the SELECT.
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you could do this:

h will contain all the unique elements in result. Or if you have a 2D Vector ( Vector of Vectors ) then you do something like:

you will end up with a Vector of Sets containing only the unique items of the Original Vector.
Jamie
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I like Jamies' choice better. But just to set the record straight the following code DOES end up with unique elements of the vector.

Regards,
Manfred.
 
Krassi Bogdanova
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again,
but my results is :
Vector results = new Vector();
[LABA001]After: [LABA001] - Vector
[LABA001]After: [LABA001] - next Vector
[LABA002]After: [LABA002] - next Vector
[LABA003]After: [LABA003] - next Vector
[LABA003]After: [LABA003] - next Vector
and I don't know how to compaire the first Vector with the next etc.
 
reply
    Bookmark Topic Watch Topic
  • New Topic