Sorting an ArrayList

Discussion in 'Java' started by flesh-bound-book, Feb 12, 2010.

  1. flesh-bound-book

    flesh-bound-book

    Joined:
    Feb 12, 2010
    Messages:
    1
    [SOLVED] Sorting an ArrayList

    Hey, guys. I have a homework assignment due later tonight and am having a bit of trouble. I have to sort an array list as part of the assignment. My understanding is that must use Collections.sort, but I can't get that to work. I am not sure if that is due to my use of generics or what. Here is my current code.

    Code:
    import java.util.ArrayList;
    import java.util.Collections;
    
    //create generic MyList class with type parameter T
    final class MyList<T extends Number>{
    
        //create ArrayList of type T
        ArrayList<T> list = new ArrayList<T>();
    
        //create an add method that adds argument of type T to ArrayList
        public void add( T new_element ){
    	list.add( new_element );
        }
    
        //create method 'largest' that returns largest value of 'list'
        public T largest(){
    	Collections.sort( list );
            //must return largest value after sorting
        }
    
        //create method 'smallest' that returns smallest value of 'list'
        public T smallest(){
    	Collections.sort( list );
    	//must return smallest value after sorting
        }
    }
    
    public final class GenericsHW{
    
        public static void main(final String[] args) {
    	MyList<Number> list = new MyList<Number>();
    
    	list.add( new Integer( 10  ) );
    	list.add( new Double( 1.2 ) );
        }
    
    }
    and the error messages I am getting are:

    Code:
    /MyDev/java/HW3/GenericsHW.java
    /MyDev/java/HW3/GenericsHW.java:30: cannot find symbol
    symbol  : method sort(java.util.ArrayList<T>)
    location: class java.util.Collections
    	Collections.sort( list );
    	                ^
    MyDev/java/HW3/GenericsHW.java:36: cannot find symbol
    symbol  : method sort(java.util.ArrayList<T>)
    location: class java.util.Collections
    	Collections.sort( list );
    	                ^
    
    I've looked at quite a few explanations of sorting arraylists, and they all achieve sorting with Collections.sort( ArrayList );, but this isn't working for me. They all used string values for sorting and I am using numbers, but it seems like Collections.sort would be able to sort values of the Number type. Any help would be great. Let me know if I left out something you need to know.
     
    Last edited: Feb 13, 2010
    flesh-bound-book, Feb 12, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,744
    Natty Gur
    Aug 19, 2003
  2. Rob G
    Replies:
    2
    Views:
    14,538
    Rob G
    Jun 15, 2004
  3. Kaidi
    Replies:
    4
    Views:
    2,391
    Kaidi
    Jan 3, 2004
  4. xz
    Replies:
    16
    Views:
    2,381
  5. Philipp
    Replies:
    6
    Views:
    928
    Arne Vajhøj
    May 28, 2008
Loading...

Share This Page