The Artima Developer Community
Sponsored Link

Java Answers Forum
sorting arrays

2 replies on 1 page. Most recent reply: Mar 5, 2002 1:24 PM by Alain Ravet

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
jayakumar perumal

Posts: 16
Nickname: jai
Registered: Feb, 2002

sorting arrays Posted: Mar 5, 2002 1:37 AM
Reply to this message Reply
Advertisement
i would like to sort an array containing pixel values. i dont want the same values to get repeated. can anyone help me out how to do this.
thanx
jai


Bruno Baguette

Posts: 3
Nickname: bouchon
Registered: Mar, 2002

Re: sorting arrays Posted: Mar 5, 2002 10:29 AM
Reply to this message Reply
> i would like to sort an array containing pixel
> values. i dont want the same values to get repeated.
> can anyone help me out how to do this.

If you want to never have any duplicate entry, maybe you should use a Set...

If you just want to have the list of the different values which exist in your array, so try this piece of code :

Vector UniqueValues = new Vector();
 
for (int i = 0; i < YourCurrentArray.length; i++)
{
   if (! UniqueValues.containsElement(YourCurrentArray[i]))
   {
      // IF the current value in the array is not
      // already in the vector than the latest value
      // THEN copy this value inside the Vector
      UniqueValues.addElement(YourCurrentArray[i]);
   }
}

Hope this help !

Alain Ravet

Posts: 19
Nickname: aravet
Registered: Feb, 2002

Re: sorting arrays Posted: Mar 5, 2002 1:24 PM
Reply to this message Reply
So, you want to sort this 4 pixel array, and only keep the 3 unique elements, and sort them?

        Pixel[] arrayPix = new Pixel[4] ;
        arrayPix [0] = new Pixel(1,3) ;
        arrayPix [1] = new Pixel(1,1) ;
        arrayPix [2] = new Pixel(1,1) ;
        arrayPix [3] = new Pixel(1,2) ;


1? use Set, to keep only unique pixels
        final Set   set     =   new HashSet ()          ;
        set.addAll(Arrays.asList(arrayPix)) ;


2? use Arrays.sort () to sort the pixels
        final Comparator c = new Comparator(){
            public int compare( Object o1 , Object o2 )
            {
                final Pixel pixel  = ( (Pixel ) o2 );
                final Pixel this_  = ( (Pixel ) o1 );
                final int rad1    = pixel.x + pixel.y;
                final int radThis = this_.x + this_.y;
                return radThis - rad1 ;
            }
        };
        Arrays.sort(pixels, c);


Don't forget to override, in the class Pixel, equals() and hashCode(), for the Collection API to work correctly:
public class Pixel
{
 
    public Pixel( int i_x , int i_y )  {
        x = i_x;
        y = i_y;
    }
 
    public int x ;
    public int y ;
 
    public boolean equals( Object obj )
    {
        final Pixel pixel = ( (Pixel ) obj );
        return this.x == pixel.x && this.y == pixel.y;
    }
 
    public int hashCode()
    {
        return x +  y ;
    }
 
 
    public String toString()
    {
        return "[Pixel: x=" + x + ",y="+y+ "]";
    }
}

Flat View: This topic has 2 replies on 1 page
Topic: Help with Java comm API Previous Topic   Next Topic Topic: MD5 password encryption like Linux

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use