Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Collections

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Collections

    I study a tutorial about JAVA and this was an example of it about collections.However when i try to call method sort,the system can not find it.Why?I have imported java.util.*

     
    package collections;
     
    import java.util.*;//library
     
    public class Collections {
     
        //Purpose:Create an ArrayList which stores strings
        public static void main(String[] args) {
            List<String> l = new ArrayList();
     
            String first="Hello";
     
            l.add(first);
            l.add(" world");
            l.add("JAVA");
     
            l.set(2,"JAVA2");//set modifies a value of the list
     
            Collections.sort(l);//sort the list
     
            System.out.println(l);
            System.out.println("Size of the list = " + l.size());
     
            int i = l.indexOf(" world");//will return the position of <world>
            System.out.println("String <world> is at " + i + " cell");
     
            l.remove("JAVA2");//will remove the first appearence of <JAVA2>
     
            l.clear();//removes all the elements of the list
            if(l.isEmpty()) 
                System.out.println("empty list");
            System.out.println(l);
        }
    }
    The error occurs at this line Collections.sort(l);//sort the list


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Collections

    Your class's name is also Collections, try renaming your class and see what happens

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Collections

    Do not write classes with names that conflict with the java API - you've named your class Collections, and it does not have a method named sort. The java.util.Collections class does, however java will not be able to find it. Rename your class (recommended), or explicitly call the method you wish to call (java.util.Collections.sort(l))
    Edit: too slow. elamre beat me to it.

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Collections

    Hmm..I see what happened.Thank you very much!

Similar Threads

  1. collections
    By koushik in forum Collections and Generics
    Replies: 2
    Last Post: January 14th, 2012, 12:06 PM
  2. Reg: Collections
    By vddmanikanta in forum Collections and Generics
    Replies: 1
    Last Post: July 17th, 2011, 09:33 AM
  3. Collections
    By pokuri in forum Collections and Generics
    Replies: 4
    Last Post: June 3rd, 2011, 09:08 AM
  4. collections
    By bardd in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2011, 09:31 AM
  5. Help me to Choose Collections
    By kathir0301 in forum Collections and Generics
    Replies: 1
    Last Post: December 3rd, 2010, 10:14 AM