Solved

Java Collections sort by name - syntax error

Posted on 2012-03-15
4
267 Views
Last Modified: 2012-03-15
Hi experts,

I have a problem using List, Collections.sort() <== got a syntax error

I have a List of data like below:

         SchoolContactInfo schoolContactInfo [] = {
                  new ArtStudent("Paul", "987654561"),
                  new ArtStudent("Avie", "987654561"),
                  new MathsStudent("John","98769832")};

         List<SchoolContactInfo > mylist = new ArrayList<SchoolContactInfo >();
         mylist.add(schoolContactInfo [0]);
         mylist.add(schoolContactInfo [1]);
         mylist.add(schoolContactInfo [2]);

                    Collections.sort(mylist);

What I want the output sorted by name using Collections sort but i got sysntax saying

SchoolContactInfo is not valid substitute of bounded parameters

My other class like below:

public abstract class SchoolContactInfo {
      private String name;
      private String phone;

      // Constructor
      public ContactInfo(String Name, String Phone) {
            name = Name;
            phone = Phone;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      public String getPhone() {
            return phone;
      }

      public void setPhone(String phone) {
            this.phone = phone;
      }
}

public class ArtStudent extends SchoolContactInfo {
      // constructor
      public FriendA(String name, String phone) {
            super(name, phone);
      }
}

public class MathsStudent extends SchoolContactInfo {
      // constructor
      public FriendA(String name, String phone) {
            super(name, phone);
      }
}

===
please correct my syntax. notes i want the output of student sorted by the name
0
Comment
Question by:unistudent
  • 3
4 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37727226
You class
SchoolContactInfo

should implement Comparabale interface
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37727240
this compiles without any error:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class School {

    public static void main(String[] args) {

          SchoolContactInfo schoolContactInfo [] = {
                  new ArtStudent("Paul", "987654561"),
                  new ArtStudent("Avie", "987654561"),
                  new MathsStudent("John","98769832")};

         ArrayList<SchoolContactInfo > mylist = new ArrayList<SchoolContactInfo >();
         mylist.add(schoolContactInfo [0]);
         mylist.add(schoolContactInfo [1]);
         mylist.add(schoolContactInfo [2]);

                    Collections.sort(mylist);

        
    }


}


  abstract class SchoolContactInfo implements Comparable <SchoolContactInfo>{
      private String name;
      private String phone;

      // Constructor
      public SchoolContactInfo(String Name, String Phone) {
            name = Name;
            phone = Phone;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      public String getPhone() {
            return phone;
      }

      public void setPhone(String phone) {
            this.phone = phone;
      }
      
     public  int compareTo(SchoolContactInfo s){
         
         return s.getName().compareTo(name);
          
      }
}

 class ArtStudent extends SchoolContactInfo {
      // constructor
      public ArtStudent(String name, String phone) {
            super(name, phone);
      }
}

 class MathsStudent extends SchoolContactInfo {
      // constructor
      public MathsStudent(String name, String phone) {
            super(name, phone);
      }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37727242
You also hneed to keep in mind that if you define constructor - it should have the same name as the class:

public class ArtStudent extends SchoolContactInfo {
      // constructor
      public FriendA(String name, String phone) {
            super(name, phone);
      }
}

Open in new window


this acuses erroer as you "contructor" has name FriendA ()
different form the name of the class ArtStudent - this causes compilatuion error - i corrected this in the code above
0
 

Author Closing Comment

by:unistudent
ID: 37727765
so clear. excellent. i am glad he helps me.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java - Why doesn't this JFrame work 3 43
github account with ecipse 1 42
HTTPSessionBindingListernter not configured in deployment descriptor 2 38
ForLoop Example 3 40
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now