Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1174

Re: Reverse sorting an array

From Fred <albert.xtheunknown0@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: Reverse sorting an array
Date 2011-09-30 16:27 -0700
Organization http://groups.google.com
Message-ID <7c66f3a8-c02b-4b5a-b62e-f0561378bfec@b6g2000vbz.googlegroups.com> (permalink)
References <8ea2add1-ce9c-423b-bdb8-92c461cf9c6d@5g2000yqo.googlegroups.com> <j64aek$23e$1@dont-email.me> <2b9bc8c9-d6de-431f-8753-fe9d400e9b1b@n36g2000yqb.googlegroups.com> <4ihc87545r018lbbk02ggp21abgngho21n@4ax.com>

Show all headers | View raw


Roedy Green wrote:
> On Fri, 30 Sep 2011 15:31:38 -0700 (PDT), Fred
> <albert.xtheunkno...@gmail.com> wrote, quoted or indirectly quoted
> someone who said :
>
> >I think the easiest way is to use sort() and then go through the array
> >from n-1 to 0.
>
> Agreed.
>
> In practice, you almost never sort ints.  You sort objects that
> contain an int field plus some other data dragged along with it.
> <snip>

Um, I wrote a test program to sort a small array of positive integers
and that worked...
But I must be missing something obvious here - why does this print
zeroes?

class InputCounter {
    private static void printArray(int[] array, int length) {
	for (int i = 0; i < length; i++)
	    System.out.print(array[i] + " ");
	System.out.println();
    }

    public static void main(String[] args) {
	final int MAX_NUMBERS = 50;
	final int EXIT = -99;

	int positivesIndex = 0; // number of positive integers entered so far
	int input; // initialise for the for loop
	int[] positives= new int[MAX_NUMBERS];
	boolean isDone = false;
	Scanner scan = new Scanner(System.in);

	for (int i = 0; i < MAX_NUMBERS && isDone == false; i++) {
	    System.out.print("Please enter an integer (-99 to exit): ");
	    input = scan.nextInt();

	    if (input == EXIT)
		isDone = true;
	    else if (input > 0)
		positives[positivesIndex++] = input;
	}
	printArray(positives, positivesIndex);
	Arrays.sort(positives);
	printArray(positives, positivesIndex);
    }
}

TIA,
Fred

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Reverse sorting an array Fred <albert.xtheunknown0@gmail.com> - 2011-09-30 04:03 -0700
  Re: Reverse sorting an array Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-30 07:50 -0400
    Re: Reverse sorting an array Fred <albert.xtheunknown0@gmail.com> - 2011-09-30 15:24 -0700
      Re: Reverse sorting an array Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-30 20:50 -0400
        Re: Reverse sorting an array Roedy Green <see_website@mindprod.com.invalid> - 2011-10-01 19:38 -0700
          Re: Reverse sorting an array Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-01 22:58 -0400
            Re: Reverse sorting an array Lew <lewbloch@gmail.com> - 2011-10-01 20:21 -0700
    Re: Reverse sorting an array Fred <albert.xtheunknown0@gmail.com> - 2011-09-30 15:31 -0700
      Re: Reverse sorting an array Roedy Green <see_website@mindprod.com.invalid> - 2011-09-30 15:45 -0700
        Re: Reverse sorting an array Fred <albert.xtheunknown0@gmail.com> - 2011-09-30 16:27 -0700
          Re: Reverse sorting an array Patricia Shanahan <pats@acm.org> - 2011-09-30 16:58 -0700
    Re: Reverse sorting an array Roedy Green <see_website@mindprod.com.invalid> - 2011-09-30 15:43 -0700
      Re: Reverse sorting an array Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-30 20:57 -0400
  Re: Reverse sorting an array Roedy Green <see_website@mindprod.com.invalid> - 2011-09-30 04:58 -0700
    What qualifies as a constant ? (Was: Reverse sorting an array) Mayeul <mayeul.marguet@free.fr> - 2011-09-30 14:45 +0200
      Re: What qualifies as a constant ? (Was: Reverse sorting an array) Lew <lewbloch@gmail.com> - 2011-09-30 07:39 -0700
        Re: What qualifies as a constant ? (Was: Reverse sorting an array) Mayeul <mayeul.marguet@free.fr> - 2011-09-30 17:15 +0200
          Re: What qualifies as a constant ? (Was: Reverse sorting an array) Lew <lewbloch@gmail.com> - 2011-09-30 08:42 -0700

csiph-web