×

# In-built useful Java functions

 0 When I was extremely new to CP, I used to manually sort the arrays(by typing the whole sorting code) instead of using the Java in-built Arrays.sort(array_name) of the util package. I would do a similar thing for binary search, until I came to know of Arrays.binarySearch(array_name,key) of the util package. Programmers, I want to know whether there are any other such short-cut array functions, String functions, techniques,tricks, anything that you use that saves a lot of time or code writing and would help newbie coders like me as well as anyone who may not be aware of them. You can also mentions short-cuts of other languages to help the users coding in them. Thanks in advance :) asked 05 Mar '17, 08:53 681●12 accept rate: 14%

 2 The best place to find convenient in-built functions is of course the language documentation. Here are Javadoc links for the Arrays and String class. You will find all the available functions listed along with their purpose, parameters, and return value. Go through them and take note of what may be useful to you. Since you are new you wouldn't be requiring these at the moment, but in the future you will likely have to use a Collection. Some useful Collections are ArrayList, ArrayDeque, PriorityQueue, HashSet and TreeSet. HashMap and TreeMap are also useful. StringBuilder is another useful class. If all these seem too overwhelming, don't worry, you don't need these right now. The Java library is pretty big :P On the plus side, because it is so big, it will often have something that matches a functionality you are looking for. Hope this helps! answered 05 Mar '17, 10:25 6★meooow ♦ 7.1k●7●18 accept rate: 48% @meooow- THAT LIST HOLYSHIT!! Wow XD Where do you find these stuff? THATS AWESOME!!! _/_ Java Pro Meow. (05 Mar '17, 10:29) Hey, Thanks :) (05 Mar '17, 11:07) Actually I am from an CISCE school like @akashbhalotia so I have faced his situation too :) (05 Mar '17, 21:23) meooow ♦6★
 1 Ahh! The memories! I remember something similar happening with me too. I was participating in a contest, and the problem included sorting the array and doing some operations, and I was getting TLE because of my slow sorting algo....and in the editorial I saw that "Arrays.sort" function and I was like "WTF?!" Its good to inquire and know about these things. However, I am afraid that the list is toooo long to be stated here. Hence I would request you to go through string fucntions list For others, please refer to meow's answer, nothing that I give could be more detailed than that! . Pay special attention to array and string methods, they are what mostly save your life. (Array.push, pop and sort should be known at least. They are quite useful!) If, after seeing the list, you have any query/doubt/need clarification, feel free to ask here again. :) answered 05 Mar '17, 10:03 15.4k●1●20●66 accept rate: 18% Thanks :) I'm sure that others too shall find these useful. Our mere school knowledge often lets us down in CP. (05 Mar '17, 11:08) I agree. Whats worse is that if you use these stuff in school papers, they give zero citing "Out of Syllabus" thing. That's sad, but I hope you enjoy the time you spend on Computer Science and Coding :) (05 Mar '17, 11:11) 1 That's true. Yup, I love Computer Science and Maths. Coding and algorithms fascinate me a lot. Competetive Programming has that thrill factor- especially short contests. They are fast and thrilling. Often it's nail-biting, when only 5-10 minutes are left and you are trying your best to get your solution accepted. Long contests teach us a lot :) (05 Mar '17, 11:25) 1 Yes!! BTW, CS and Maths? You're gonna go far kid :D. Knowing maths is a good advantage in CS. Yes short contests are thrilling, and long contests are very, very good for knowledge. I still remember how anxious I was during last minutes when I scored my final 15 marks (partial marking) out of 50 marked Q, earning a silver medal (hackerrank contest give medals based on ranking. Top 12 % get medal, and by those 15 marks I slipped into that :p) I am happy you got your passion clear at this age dear! It took me 2 years to find I love CS and coding. :) (05 Mar '17, 11:34) 1 Haha, you praise too much. What's the thing about passions? A person may have a liking today which may seem like a passion, but may change tomorrow. Passions often deceive :) But yeah, I've really liked Computer Science since class 8 and Maths since long back. I'm currently a class 11 Science student and the thing that distinguishes Maths and CS from Chem and Physics is the number of "AHA!" and "WOW!" moments it gives- you know, when you have been thinking about a problem for a long time, staring at it, and then, suddenly, you get the solution! Such moments are priceless... (05 Mar '17, 11:52) I feel you bro ^^ Passions which are deceptive are not passions, they are just 'liking'. Its important to differentiate between a 'liking' and a 'passion'. And yes, CS has a LOT of wow moments XD (05 Mar '17, 12:44) 1 @vijju123 I think you mistakenly set the link in your answer for Javascript functions instead of Java functions. (05 Mar '17, 21:21) meooow ♦6★ Oh my god! shit!! Yes I did. Thanks for pointing it out dear!! Rectifying the error immediately :) (Actually I had the tab of javascript open, cause m learning it along with HTML. Lol me XD) (05 Mar '17, 21:32) showing 5 of 8 show all
 1 If you are new in competitive programming then i suggest you to switch your java to C++ language as C++ is best and best among all other languages specially in Competitive programming. You can estimate this by looking submission of languages at any platform like codechef, SPOJ, Hackerearth, COdeforces etc. Even one of the top programmer Gennady Korotkevich also used C++ as a special tool In programming. You can know more about this here:- answered 05 Mar '17, 14:45 2.8k●1●4●19 accept rate: 16% 2 You can't forget Petr and uwi. It isn't bad to use Java either. All that matters is your hold on Algorithms and Data Structures. And the functions that are listed below are available in Java too. Please don't misguide people or build such stereotypes that C++ is better than Java in competitive programming. There are perks of using Java in comparison to C++. (05 Mar '17, 22:10) 1 Exactly. Most sites like Codechef focus on the algorithmic complexity as much as possible by allowing many languages and setting appropriate relative time limits. It is not necessary to learn a completely new language for competitive programming. Choice of language should be upto the programmer. (05 Mar '17, 22:21) meooow ♦6★
 1 @bansal1232 I agree with you. c++ is best for competitive coding and easy to learn. There are some useful data structures in c++ you should learn. array vector stack heap map pair Some Short-Cut functions in c++ vect.sort( vect.begin() , vect.end() ); // Sorting a vector vect.clear(); // Clearing a vector vect.size(); // Returns the size of a vector  There are a lot of functions I can not list them here. You can go to http://www.cplusplus.com/ and search for the reference you want. answered 05 Mar '17, 21:37 4★only4 1.5k●2●12 accept rate: 17%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,912
×1,477
×1,302
×521

question asked: 05 Mar '17, 08:53

question was seen: 1,629 times

last updated: 05 Mar '17, 22:21