Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Default Arraylist sorting in alphabetical order

    I need help with an assignment. Basically the assignment is to complete the addWord method of the controller class so that it adds the parameter word into the instance field ArrayList called words in the correct alphabetical order.

    However, if word is already in ArrayList, it should be removed completely.


    The method is as follows:

    private void addWord(String word) {
    // Complete this method to add word in the correct Alphabetic order in the list
    // if the word is already in the list, it should be removed (no duplicates)

    //this should be the last line of the method (don't touch it)
    updateList();
    }





    How should the method be completed?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: Arraylist sorting in alphabetical order

    First I'd check, whether the word is in the list already and remove it. If not, iterate over the list and compare the list element with the new word and insert it at the position where the result from the comparison changes.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: Arraylist sorting in alphabetical order

    if word is already in ArrayList, it should be removed completely.
    That's a strange requirement. For example if the list contained only A and B and you try to add A and B then after those two add attempts, the list would be empty.
    Of if you add a word three times, the end results would be the word is in the list: added, removed, added
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,050
    Rep Power
    4

    Default Re: Arraylist sorting in alphabetical order

    Yeah well seems like a school assignment or part of some sort of training - I have yet to see such an assignment make sense to be honest ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Arraylist sorting in alphabetical order

    // Complete this method to add word in the correct Alphabetic order in the list
    // if the word is already in the list, it should be removed (no duplicates)
    This makes no sense. If it is in the list, then remove it. But then you say no duplicates. Are you certain it isn't more like
    if it is in the list, then just ignore it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    kire9dk is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Default Re: Arraylist sorting in alphabetical order

    Hello,

    I read your problem but it is not 100% clear. However, if you need some List to be sorted then you can use Collections.sort()
    Collections (Java Platform SE 6). If you have custom class, then your class needs to implement Comparable interface.
    For strings this interface is already implemented. And for not inserting part, you can try indexOf(Object object), contains(Object object) or remove(Object object), List (Java Platform SE 6). Again if you have custom class then in order this methods to work you will need to Override equals.

    This much I am able to help you. But again, all this things can easily solve your problem

Similar Threads

  1. Alphabetical Sorting
    By Zelaine in forum New To Java
    Replies: 8
    Last Post: 10-30-2013, 02:26 PM
  2. Replies: 2
    Last Post: 10-05-2013, 05:40 AM
  3. array to sort names in alphabetical order
    By leoshiner in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 12:28 PM
  4. How to sort array objects into alphabetical order...
    By lisalala in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 11:01 AM
  5. Sorting in descending order
    By santanu in forum New To Java
    Replies: 6
    Last Post: 11-26-2008, 11:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •