We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Java : ArrayList using Comparator

gbilios
gbilios asked
on
Medium Priority
8,136 Views
Last Modified: 2008-01-09
I am using two methods of sorting an arraylist.  the output from the compiler says that the the CompDemo is using unchecked or unsafe operations.  not sure what this is referring to.  

 import java.util.*;  
   
    class MyComp
          implements Comparator
    {
     
       public int compare(Object o1, Object o2) {
       
           String aStr, bStr;
           
           aStr = (String) o1;
           bStr = (String) o2;
      
            return aStr.compareTo(bStr);
   
   }
   
       public static List orderCallNumbers(ArrayList lst)
       {  
             Collections.sort(lst, new MyComp());
              
      return lst;  
      }
                           
   }

    public class CompDemo
   {
   
       public static void main(String args[])
      {
         ArrayList ls = new ArrayList();
         
                  /* dummy library call numbers */
            ls.add("L234f");
            ls.add("A2343");
            
                  /* let the collections.sort do the job */
            System.out.println(new MyComp().orderCallNumbers(ls) + " " +
                        " \tUsing the Collections.Sort");
     
            /* you can alternatively use the Arrays.sort(param) method to sort in asc */
                  
                  Object ia[] = ls.toArray();
                  Arrays.sort(ia, new MyComp());
                  
                  for(int index = 0; index < ia.length; index++)
                  {
                        System.out.print(ia[index]+ " ");
                  } /* end for */
      System.out.println("\tUsing the Array.Sort");
      } /* end main */
   } /* end class */
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
That's just a warning. You can ignore it, or compile

javac -source 1.4 X
CERTIFIED EXPERT
Top Expert 2016

Commented:
You're using a 1.5 compiler. It uses strong type checking through generics. To avoid warnings, you need to use generics
CERTIFIED EXPERT
Top Expert 2016

Commented:
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
java 5 does more type checking

>  ArrayList ls = new ArrayList();

change to:

 ArrayList<String> ls = new ArrayList<String>();

and

> class MyComp implements Comparator

to:

class MyComp implements Comparator<String>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Don't ignore them, fix them. You'll have better code and get into better programming habits :)
And that means less unexpected bugs in the future
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
What the changes above do is tell the compiler that your list will only contain Strings, and that your Comparator compares Strings. Making things a lot more type safe as the compiler can catch any errors earlier, rather than at runtime.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
your compareTo() method can also now become:

     public int compare(String aStr, String bStr) {
          return aStr.compareTo(bStr);
   }

(which is the default string sort behaviour btw, I assume you plan to change that)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> your compareTo() method can also now become:

that of course should have said compare() ;)
CERTIFIED EXPERT
Top Expert 2016
Commented:
The following will remove all warnings:


import java.util.*;

class MyComp implements Comparator<String> {

      public int compare(String s1, String s2) {
            return s1.compareTo(s2);
      }

      public static List<String> orderCallNumbers(ArrayList<String> lst) {
            Collections.sort(lst, new MyComp());
            return lst;
      }

}

public class CompDemo {

      public static void main(String args[]) {
            ArrayList<String> ls = new ArrayList<String>();

            /* dummy library call numbers */
            ls.add("L234f");
            ls.add("A2343");

            /* let the collections.sort do the job */
            System.out.println(MyComp.orderCallNumbers(ls) + " "
                        + " \tUsing the Collections.Sort");

            /* you can alternatively use the Arrays.sort(param) method to sort in asc */

            String ia[] = (String[])ls.toArray(new String[0]);
            Arrays.sort(ia, new MyComp());

            for (int index = 0; index < ia.length; index++) {
                  System.out.print(ia[index] + " ");
            } /* end for */
            System.out.println("\tUsing the Array.Sort");
      } /* end main */
} /* end class */
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
>  Object ia[] = ls.toArray();

That can also now be cahnged to the follwoing:

 String ia[] = ls.toArray(new String[0]);

Author

Commented:
I am not familiar with the java 1.5.  Thank-you very much.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
No worries :)
Let me know if I can help further.

CERTIFIED EXPERT
Top Expert 2016

Commented:
gbilios -can you explain why you ignored all my comments ?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> gbilios -can you explain why you ignored all my comments ?

probably because they didn't show how to fix it, and to just ignore them ;)
CERTIFIED EXPERT
Top Expert 2016

Commented:
I posted complete code of how to fix it in case you didn't notice
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
which was a copy of what I had already posted.
CERTIFIED EXPERT
Top Expert 2016

Commented:
I can assure you your comments do NOT fix it so there are no warnings
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>I can assure you your comments do NOT fix it so there are no warnings

Therefore, gbilios, if you no longer have warnings, you must be using my code, so why am i not in a points split please?

Author

Commented:
there are no more compile warnings..the reason I don't just want the code - i wanted a reason why, too.  there is no option to split the points.

CERTIFIED EXPERT
Top Expert 2016

Commented:
>>the reason I don't just want the code - i wanted a reason why, too.  

I actually not only gave you both, but posted a link to further reading

>>there is no option to split the points.

That's incorrect

Author

Commented:
tell me how to split the points and i'll do it now
CERTIFIED EXPERT
Top Expert 2016

Commented:
You needn't do anything until you receive a notification from a page editor

Author

Commented:
that case, i need to contact the page editor and explain you want your fair share.



CERTIFIED EXPERT
Top Expert 2016

Commented:
That's already been done - don't worry

Author

Commented:
matter of interest. how do you split the points?
CERTIFIED EXPERT
Top Expert 2016

Commented:
You'll find a link that says 'Split Points'

Author

Commented:
okay..cool
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)

Author

Commented:
feel better now
:-)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> I can assure you your comments do NOT fix it so there are no warnings

They fixed all the original warnings. Which you blatantly copied, which if you read your member agreement is not permittered.
And the warning caused by my suggested changes I posted a fix for (which you actual got wrong anyway).
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Now I'd like to know why CEHJ was awrded the lion share of the points after copying all of my suggestions?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>They fixed all the original warnings.

As i've said, your alterations do NOT produce the desired result

>>Which you blatantly copied

ROTFL - do you imagine i need to copy your code?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Now I'd like to know why CEHJ was awrded the lion share of the points after copying all of my suggestions?

Perhaps because i fixed all of the warnings and answered the question fully?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
"If you basically agree with another comment but have something more to add, remember to give credit for the original suggestion -- mention that Expert by name -- in your post."
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.