• Post Reply Bookmark Topic Watch Topic
  • New Topic

Intuition in Java  RSS feed

 
Umair Khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Query Part 1:
I know Matlab and also know basic C++. I am good at making algorithms and finding logics. I am finding one problem while working with Java.
As one of the examples, it occurs frequently that while programming I have an array and I want to sort it. I know that there must be a method that would do it for me itself. How can I know that such a sorting algo exists in Class Arrays and method sort i.e. Arrays.sort?
I mean in Matlab i would have searched in the help section for the word 'sort' and would have found its use straightaway. Is there any such method to find in Java as well? I know i can google but I want to do it on my own and not take google's help. I hope I have clarified my question.

Query Part 2:
I want to flip an integer array. I know how to do the coding. But I want to use some method that someone else has already defined so that I don't need to reinvent it again. I don't find any flipping method in class Array. Does such a method exist in some other class? If yes, Please guide me what are the steps to find such a method in the whole Java library?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to learn your way around the API documentation; look for the Arrays class in the lower left panel (not "Array" which is different), and see what methods it has for sorting or reversing arrays. When you read the appropriate page, you can see which methods are available, and which are not available.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With practice and continuous exposure to the Java API you will get used to the vast API and will know which method is available in which package and how to search it. I would recommend you not to use an IDE during your initial stages of programming in Java. Also you can download the Documentation (search for .chm format of the documentation) and refer to it for any method you want to know. As pointed out in a earlier post- You have to learn you way around the API.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's an Index link at the top of the API.
 
Umair Khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tips. I found the chm format documentation of great use.
After going through google and the documentation, I deduced that there exists no method to reverse an integer array. There is method to do so for strings but not for integer array.
I would need to do that manually with a for loop.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you mean by 'reverse' it? Would sorting it then iterating backwards solve it? In other words, if I have the array {2,3,5,7,11,13}, wouldn't simply iterating

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's very easy to reverse an array. Here is a simple way to do itTry it and see.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier wrote:Try it and see.
. . . and look closely at Fred Rosenberger's signature. Then see if you can find the errors in what I posted (there are at least three serious errors in it)
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I corrected 0l to 0; in that code, which was a typographical error, not one of the logic errors I expect you all to find.
 
Umair Khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It's very easy to reverse an array. Here is a simple way to do itTry it and see.


The real question wasn't really to reverse/flip an array. It can be done the way you did it. What I needed to know whether there exists any prebuilt method that does the same thing. I really believed that a reverse method should have been present in the Arrays class so that we just needed to type Arrays.reverse(intArray)
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umair Khan wrote:...a reverse method should have been present in the Arrays class so that we just needed to type Arrays.reverse(intArray)


That is a good point about should. The question though is 'is there one?' What does the API say? If not, then you need to make one yourself - and since it is common, perhaps put it into your own utility class so you can re-use it when needed.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umair Khan wrote: . . . It can be done the way you did it. . . .
No, I can't. Read my other post from earlier today.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umair Khan wrote: . . . It can be done the way you did it. . . .
No, I can't. Read my other post from earlier today.
 
Umair Khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Umair Khan wrote: . . . It can be done the way you did it. . . .
No, I can't. Read my other post from earlier today.


Haha. You didn't do it but actually you can. Three errors are ofcourse you didn't introduce an intermediate temp variable, missed a '-1' in the third line of the code and '-i' in the 4th.

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The - 1 you picked up is another typo; it should read - i. Sorry.

There is another error, just as serious, and even more annoying, which you haven't noticed yet
 
Umair Khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The - 1 you picked up is another typo; it should read - i. Sorry.

There is another error, just as serious, and even more annoying, which you haven't noticed yet


Yeah I missed it. The loop should run for myArray.length/2.
This code should be set up as a test for students in school.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For students? Of course. Aren't most of the people on "beginning Java" students? I hope they have read it and will remember next time they have to write a reverse method
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!