Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
My question is, if I want to check a character (only one character to check) of a string in a particular index which method is very efficient charAt() or startsWith(). I mean in comparison of time complexity as far I can guess startsWith() gets more time than charAt(). Because startsWith() needs to check a set of characters, but charAt() just need to check only one character.
Now tell me your opinion… what you think about which is efficient to use to check only one character?
Both methods can be used to check a specific character for its value.
charAt() directly returns the char at the requested index, startsWith(prefix, index) will return true if you provide corresponding arguments.
The major difference is that the second approach has a bit more of overhead.
So, theoretically option 1 has slightly better performance. But beyond that, you rather pick option 1 because that just does what you want in the most clear way.
The really important difference is not about performance, but about your code communicating your intent. So, albeit is possible to use startsWith() to do what you want, it is simply counterintuitive to use it that way.
if I want to check a character(Only one character to check) of a string in a particular index
You answered by yourself. If you need to check that in a particular index, you can't use startsWith() , because you can't choose the index.
They do different jobs, so , based on your question, always use charAt()
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a check in a loop where I have to check if the number of occurrence is less than 10 which could be written as either
if(occ < 10){
}
or
if(checkIfOccurencyIsLessThan10(occ)){
values.add(current+"0"+occ);
}
else{
values.add(current+occ);
}
I'm reading Clean Code a handbook of agile software craftsmanship, where they say a method should do the least amount, and code should be hacked up into more pieces. Is this necessary right here? I'm trying to get a better grasp on how long a method should be, and how much it should be doing.
It depends on if this condition is spread across multiple pieces of code, and if this check could change in the future to include checking additional edge cases. If both of those things are true or could be true, then sure, extracting the check to its own function is wise. However, I would definitely say you should rename the function to not specify the functions implementation, because that defeats the purpose of being able to change out the conditional, right? Naming it something like occurenceNeedsZero is a much more flexible solution. Because if you come up with other use cases that need checking you can add them to this function as well!
However, if your question is "should I always make a simple conditional check such as "is x < 10" into its own function, then I would say no. That would be overengineering, in my opinion. Functions should be used to 1) separate logical portions of code, 2) increase readability, or 3) extract small pieces of code that are spread across multiple locations and likely to change in the future, as it simplifies future refactoring.
There are probably more cases than those 3, but those are the big ones.
It's better to use a static final variable to store this 10, instead hard code.
If there are other places need to check if occ < 10, you need extract it as a method. Otherwise it is unecessary.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to finish an assignment in my intro to Java course, and I have some questions. First off, what does it mean when there is a -- in FRONT of an int value? Also what is a String Builder? I had some help but want to understand what it is I'm using in the code. Thanks.
The -- in front of a value simply means subtract 1 from it. Similarly, ++ in front of a value means add 1 to it.
If you write ++
before the number it is called prefix operator and if after then its post fix
preFix: ++a will increase the value before using it, will first increase and then use it.
postFix a++ will first use it and then use it, for later use you will get the incremented value.
-- is a predecrement
Java StringBuilder class is used to create mutable (modifiable) string.
A String is immutable i.e string cannot be changed once created and everytime a value is change it create new string.
But in case of StringBuilder which is mutable string can change.
My experience is mostly with C# not Java, but in C# strings cannot be changed, when you concatenate two strings like "hello" + "world" you do not change either string, you create a new one and the old two still exist. If you need to do this many times (dozens or hundreds) it can use a lot of memory. A StringBuilder allows you to conserve memory by appending characters to the same block of memory while you are building your string, and then you can turn the result into a normal string for passing around to other functions.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a long with a single bit set and I need to know which it is, e.g. the index. I need to calculate this as fast as possible.
The naive idea is to divide by 2 and check whether the result is 1. But this would need up to 63 iterations (worst case).
My next idea was to make like a binary search, e.g. to look wether it is bit 63-32 or 31-0 then 63 - 48, 47 - 31, 31 - 16, 15 - 0 and so on having many if-else statements, but this gives me hell of a bunch of code...
Furthermore I'd like to minimize object creation and memory used. You might suggest that I'm wrong then with Java and should use perhaps C/C++. Well it's for a school competition and I don't have a choice :)
I'd like to see some sample code!
Use Long.numberOfTrailingZeros - this will be exactly the index you are looking for.
Long.numberOfLeadingZeros can be also useful if you count bits starting from the highest one.
Both methods are JVM intrinsics, i.e. they are treated specially by JIT compiler. These methods are translated to a special CPU instruction (TZCNT / LZCNT) and thus are very efficient.
You could prepare a Map<Int, Int>, holding the number of the set bit for each possible value, but I'm not sure if it is really faster than a loop.
Maybe bit shifting is faster than dividing by 2.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Think of the following scenario:
Application receives a list of regex from server (HTTP GET returns a List with each item indicating a regular expression.);
User input text needs to be validate against these expressions;
Application runs on Android, so memory is an issue;
List of expressions is not frequently changed.
What would be better:
Cache several Pattern objects, each one containing a single regex returned from server;
Concatenate the regex - (REGEX1)|(REGEX2)|(REGEX3)...|(REGEXN) - and maintain a single object on memory? - refreshing it whenever a single regex is added or removed, which doesn't happens very often.
I don't imagine there is a way to answer this question without having a specific list of regex's and the list of input. Because, each regex/input combination is going to result in a different amount of memory used. Here is what my instincts tell me:
Evaluate the Regex's one at a time. In the "OR" scenario, the regex must simultaneously evaluate all of the OR'd expressions, so that would take more RAM, I believe.
Order the Regex's in order of either: (a) Likelihood to match, so that you can abandon evaluating the rest of the regex's or (b) Early non-matching, so that the regex can be quickly discarded as never going to match (for example "^a" only requires evaluating the first character of the string where as "a" requires searching the whole string for an "a".)
Ultimately, only testing can really tell you what takes more time/memory, I'm afraid.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
hi i have a java programming assignment wich include 3 exercice i have done 2 already but in the third one i am dont know wich data type i should use
here is the question :
Write a java program that reads from the user the course code, section and the scores of a student for three exams representing two midterms and a final. The percentage of each of the three exams is of fixed value 30, 30 and 40 percent respectively. Your program computes the final average grade and displays it along with the course code and the section.
Remark: All data, except for the average, must be whole numbers and you should use the most efficient data type that is suitable for this specific exercise.
Sample Run:
Enter your course code: CSCI250
Enter your section: E
Enter the scores of the tests and the final: 97 83 77
CSCI250 E 84.8 (result)
so what i want to know is what is the preferable data type to use for course code ? and char is the one that i should use for section right ?
If you're capturing user input, use a String for everything.
Reason? You may request a number, but the user can type anything. Your code needs to handle bad input.
I think you can use String as data type for Course Code. You can write both numbers and letters by using it. And for section, yes, char will be suitable for it.
Use a String for arbitrary text.
If the section code is always present1 and is never more than character than a char can be used.
However, I would still use a String for consistency, flexibility, and easy of use. The teacher may prefer this based on the "efficient"cy they are going for.
1The is a soft "always": while char cannot represent null a sentinel (eg. '\0' or ' ') can be used to indicate 'no section specified'. Using such a sentinel to supplement null can also (but does not always) lead to more logic work - in particular when the record is displayed.
In any case, it is probably best to not switch to Character just for the null as this is most likely outside of the scope of current course work.