aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays.sort(int[], int, int) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays.sort(int[], int, int)" Watch "Arrays.sort(int[], int, int)" New topic
Author

Arrays.sort(int[], int, int)

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi, What is the meaning of the method (Arrays.sort(int[], int, int))declared in this class ? I did not understand from the Javadocs


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

That isn't very helpful. Can you quote the Javadocs and ask a specific question about the part you don't understand?
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Paul, I see this in the Javadocs "Sorts the specified range of the specified array of ints into ascending numerical order. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.)"..I did not understand the meaning of it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Do you understand the meaning of the first sentence? The first word? Surely you don't mean to say you don't understand anything at all about it?

If you explain what you do understand and what you don't understand then we can get started on explaining the latter parts.
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Sorts the specified range (1-2 index, but 2 is exclusive, means 1-1) of the specified array (intArray) of ints (33, 2, 5, 1) into ascending numerical order..I thought sorting will be done from index 1 to index 1. Because it is only 1 element, I thought output should be (33, 2, 5, 1)..If the range is 0-4(4 exclusive means 3)..which becomes 0-3, that's why sorting should be done from index 0 to 3. the output should be 1, 2, 5, 33..but my assumption is completely wrong after executing multiple options.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

So it looks like you do understand what it says, after all. But now you seem to be referring to the execution of some code and to the output of that code, which we haven't seen. And you think that the output should be something else, which you also haven't told us. That isn't very helpful.
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Paul, I mentioned what the output should be "Because it is only 1 element, I thought output should be (33, 2, 5, 1)..". May be I do not understand what you are expecting from me. I gave everything I know. All I do not understand is, how output is different than my assumption.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Harikrishna Gorrepati wrote:Hi Paul, I mentioned what the output should be "Because it is only 1 element, I thought output should be (33, 2, 5, 1)..". May be I do not understand what you are expecting from me. I gave everything I know. All I do not understand is, how output is different than my assumption.


What do I expect from you? Well, you told us what you thought the output should be. And you don't understand how the actual output is different than that. Well, I don't understand that either because I can't see the actual output. I hope that helps you to understand what I expect.
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Actual output is 1 2 5 33. but I am expecting (33, 2, 5, 1). Yes. I don't understand how the actual output is different than that. Don't know where I am going wrong.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

I agree with you about what the output of the posted code should be. And what you are actually seeing is what would happen if the entire array were sorted, right? So you should make sure that you are actually running the code you think you ran, and not some other code with "Arrays.sort(intArray)" in it instead.
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

Actual output is 1 2 5 33. but I am expecting (33, 2, 5, 1). Yes. I don't understand how the actual output is different than that. Don't know where I am going wrong.


I thing you are executing some wrong code as paul said...the actual output is what you are expecting...means 33,2,5,1
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
I am completely lost and not in sync with Paul. It would be really helpful if you/somebody can provide valid code with small explanation because you said I am executing wrong code.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

The code you posted at the beginning of the thread will produce the output you expect it to produce. You understand the documentation correctly. It will not produce the output you said it produced.

Therefore you must be running some other code.
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68



The output of this code is : 33,2,5,1
Explanation:Sorts the specified range of the specified array of ints into ascending numerical order. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty).
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Harikrishna Gorrepati wrote:I am completely lost and not in sync with Paul. It would be really helpful if you/somebody can provide valid code with small explanation because you said I am executing wrong code.


It's not much easier to show that you are not "executing wrong code" by showing us the exact codes and there outputs? What you are describing doesn't make sense, hence, we are saying you have a bug in your code. Show us the code and output for that code.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Harikrishna Gorrepati wrote:Sorts the specified range (1-2 index, but 2 is exclusive, means 1-1) of the specified array (intArray) of ints (33, 2, 5, 1) into ascending numerical order..I thought sorting will be done from index 1 to index 1. Because it is only 1 element, I thought output should be (33, 2, 5, 1)..If the range is 0-4(4 exclusive means 3)..which becomes 0-3, that's why sorting should be done from index 0 to 3. the output should be 1, 2, 5, 33..but my assumption is completely wrong after executing multiple options.


My code... modified from your code...



My output...

33 2 5 1
1 2 5 33


Looks correct to me.

Henry
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Thanks Henry. This is what I was looking for. You made it so simple
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

Harikrishna Gorrepati wrote:Thanks Henry. This is what I was looking for. You made it so simple


Yeah, but you should still show us the code ...

Harikrishna Gorrepati wrote:If the range is 0-4(4 exclusive means 3)..which becomes 0-3, that's why sorting should be done from index 0 to 3. the output should be 1, 2, 5, 33..but my assumption is completely wrong after executing multiple options.


that led to to draw the conclusion that your assumption is completely wrong. Heck, you should show us the multiple options that you tried.

Henry
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Here is the updated code with comments.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays.sort(int[], int, int)