Java : ArrayList using Comparator

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 */
gbiliosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

javac -source 1.4 X
0
CEHJCommented:
You're using a 1.5 compiler. It uses strong type checking through generics. To avoid warnings, you need to use generics
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

objectsCommented:
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>
0
objectsCommented:
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
0
objectsCommented:
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.
0
objectsCommented:
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)
0
objectsCommented:
> your compareTo() method can also now become:

that of course should have said compare() ;)
0
CEHJCommented:
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 */
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
>  Object ia[] = ls.toArray();

That can also now be cahnged to the follwoing:

 String ia[] = ls.toArray(new String[0]);
0
gbiliosAuthor Commented:
I am not familiar with the java 1.5.  Thank-you very much.
0
objectsCommented:
No worries :)
Let me know if I can help further.

0
CEHJCommented:
gbilios -can you explain why you ignored all my comments ?
0
objectsCommented:
> 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 ;)
0
CEHJCommented:
I posted complete code of how to fix it in case you didn't notice
0
objectsCommented:
which was a copy of what I had already posted.
0
CEHJCommented:
I can assure you your comments do NOT fix it so there are no warnings
0
CEHJCommented:
>>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?
0
gbiliosAuthor 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.

0
CEHJCommented:
>>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
0
gbiliosAuthor Commented:
tell me how to split the points and i'll do it now
0
CEHJCommented:
You needn't do anything until you receive a notification from a page editor
0
gbiliosAuthor Commented:
that case, i need to contact the page editor and explain you want your fair share.



0
CEHJCommented:
That's already been done - don't worry
0
gbiliosAuthor Commented:
matter of interest. how do you split the points?
0
CEHJCommented:
You'll find a link that says 'Split Points'
0
gbiliosAuthor Commented:
okay..cool
0
CEHJCommented:
:-)
0
gbiliosAuthor Commented:
feel better now
:-)
0
objectsCommented:
> 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).
0
objectsCommented:
Now I'd like to know why CEHJ was awrded the lion share of the points after copying all of my suggestions?
0
CEHJCommented:
>>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?
0
CEHJCommented:
>>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?
0
objectsCommented:
"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."
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.