Results 1 to 2 of 2
  1. #1
    chdn202002 is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default sorted, shuffled, reversed

    Hello, Iím trying to write a program that takes in numbers until a sentinel value is entered then displays a sorted, shuffled and reversed?

    So far this is what I have come up with.

    I know I need to implement the comparator but Iím not sure how too.

    Could you provide some feedback?

    Thanks



    Public class SortShuffle {
    public static void main(String[] args) {


    Collection<Integer> list = new ArrayList<Integer>();

    Scanner scan = new Scanner (System.in);

    System.out.println (" Input Number : ( 0 to exit ) ");
    int data = scan.nextInt();

    int sum = 0;
    while (data != 0) {
    sum +=data;

    System.out.println (" Input Number : ( 0 to exit ) ");
    data = scan.nextInt();
    list.add(data);
    }
    System.out.println (" The sorted numbers are ");
    Collections.sort(list);
    System.out.println (" The shuffled numbers are ");
    Collections.shuffle((java.util.List<?>) list);
    System.out.println (" The reversed numbers are ");
    Collections.reverseOrder();
    }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Rather than doing set up before the loop and then getting more input in the loop you can shorten it with something like this

    Java Code:
    int n;
    while((n=in.nextInt())!= 0){
      //do stuff
    }
    What happens when you just pass the list into sort and shuffle? Look up what the reverseOrder method it. what does reversing the list imply? Some sort of sorting perhaps? What happens if you use it's comparator to sort a list?

Similar Threads

  1. Checking to see if Array is sorted
    By pytho in forum New To Java
    Replies: 5
    Last Post: 07-06-2011, 01:39 AM
  2. Why is my ArrayList not sorted?
    By Tabula Rasa in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 08:31 PM
  3. Checking if an ArrayList is sorted
    By MonkeyGrad in forum New To Java
    Replies: 4
    Last Post: 04-13-2011, 06:27 AM
  4. Casting an int from a sorted set
    By Bmack in forum New To Java
    Replies: 2
    Last Post: 03-17-2010, 07:09 PM
  5. My doublyLinked list does not get sorted
    By hasani6leap in forum New To Java
    Replies: 0
    Last Post: 01-06-2008, 03:09 PM

Posting Permissions

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