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.

Page 2 of 2 FirstFirst 12
Results 26 to 29 of 29

Thread: Sorted list and compareTo method

  1. #26
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Sorted list and compareTo method

    And that small fix is changing the person Class?

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sorted list and compareTo method

    No the code using the compareTo method. It needs to have a reference to the name in the Person in the Node and a reference to the name in the Person from the user.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Sorted list and compareTo method

    Okay, so inside the method insert I need to have those references?

    --- Update ---

    Okay I've figured it out

     
    class PhoneBookList implements PhoneBook {
        private int size = 0;
        Node head = null;
     
        private static final class Node {
            final Person person;
            Node next;
     
            Node(Person person) {
                this.person = person;
            }
        }
     
        public boolean insert(Person person) {
            Node n = new Node(person); 
            Node p = head;
     
            if(p == null) {
                head = n;
                size++;
                return true;
            } else {
     
                Node temp = p;
                int comparison;
                while(temp.next != null) {
                    comparison = temp.person.name.compareTo(person.name);
                    if(comparison == 0){
                        return false;
                    }
                    temp = temp.next;
                }
                temp.next = n;
                size++;
                return true;
            }
     
        }
    }

    I'll go on to implement the other methods as well. Thank you for your help, you can count on me me coming back hahha

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sorted list and compareTo method

    I am Glad to hear you figured it out. I'll see you later for your next problem.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    arhzz (May 16th, 2020)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. What does compareTo method of String do ?
    By djl1990 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 18th, 2012, 03:39 AM
  2. Implementing the compareTo method?
    By colerelm in forum Java Theory & Questions
    Replies: 2
    Last Post: December 3rd, 2011, 07:47 PM
  3. Java Newbie - Sorted Linked List not inserting properly - please help!
    By bubbleboy in forum What's Wrong With My Code?
    Replies: 20
    Last Post: June 17th, 2011, 11:48 AM
  4. Replies: 3
    Last Post: June 1st, 2011, 12:47 AM
  5. Sorted Doubly linked List
    By student in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2010, 09:52 PM