java: jdk1.8 StringBuilder::newCapacity? [closed] - java

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 4 days ago.
Improve this question
private int newCapacity(int minCapacity) {
// overflow-conscious code
int newCapacity = (value.length << 1) + 2;
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
? hugeCapacity(minCapacity)
: newCapacity;
}
Why +2 when getting new capacity?
Is it for speed? Space for time?

I think it ensures the corner case of very small strings.

Related

Cyclomatic complexity value [closed]

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 5 days ago.
Improve this question
int a, b, c;
a = 1;
b = 2;
if (a > b) {
c = 23;
} else {
c = 25;
}
while (b < c) {
b = b + 1;
}
System.print.outline(“answer=“ + b);
return 0;
What's the cyclomatic complexity value for the code?
I tried calculating it and got 3 and 4, it changes as I tried different flow graph that connect to different nodes.

how to stop the code when the code has executed a certain amount of times? [closed]

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 2 years ago.
Improve this question
Name: runSequence4
Input: Intake feed, int n
Output: none
Action: Takes in an intake object and calls the method give(). It first passes the number 1 into
give and then alternates between -1 and 1. It does this n number of times.
How do I stop the loop when the code executes it "N" times?
This is what i have so far
public void runSequence4(Intake feed, int n){
for(int x = 1; ???? ; x = (x * -1)){
feed.give(x);
}
}
Just add a variable i to store the current iteration and test whether it has exceeded n
public void runSequence4(Intake feed, int n){
for(int x = 1, i = 0; i < n; x = (x * -1), i++){
feed.give(x);
}
}

Which one of these solutions has a better style/performance? [closed]

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 found an interesting exercise on codingBat and now I have a Question About the Solution. The Task was:
Given a string, return true if the first instance of x in the string is immediately followed by another x.
What I wrote is:
boolean doubleX(String str) {
return str.contains("x") ? str.indexOf('x') == str.length() - 1 ? false : str.charAt(str.indexOf('x')) == str.charAt(str.indexOf('x') + 1) : false;
}
The solution they had on their page was:
boolean doubleX(String str) {
int i = str.indexOf("x");
if (i == -1) return false; // no "x" at all
// Is char at i+1 also an "x"?
if (i+1 >= str.length()) return false; // check i+1 in bounds?
return str.substring(i+1, i+2).equals("x");
So now my Question is which solution has the better coding style? Which solution is more Beautiful or even more efficient?
Thanks for all answers
I would probably code it like this:
boolean doubleX(String str)
{
int index = str.indexOf("x");
return (index >= 0 && index == str.indexOf("xx", index));
}
Probably not the most performant, but it handles every possibility.
It can be slow if indexOf("x") and indexOf("xx") are far apart (unnecessary, long search for "xx") or if both indices are very high (long search done twice).
So another, slightly less intuitive, but more performant solution could be:
boolean doubleX(String str)
{
int index = str.indexOf("x");
return (index >= 0 && index < str.length() - 1 && str.charAt(index + 1) == 'x');
}

How to put values from ArrayList in certain ranges? [closed]

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
Is it possible to put values from my ArrayList into a specific ranges?
My ArrayList contains doubles:
0.4987873968412172
0.49949149542775684
0.4995227496071881
0.4999386804321369
0.500520813392628
0.5005912302221284
0.5006590304457654
0.5009476185730563
0.5013192031470984
And I want to divide them into (for example) two ranges to get an output:
Numbers from 0.498 to 0.499
0.4987873968412172
0.49949149542775684
0.4995227496071881
0.4999386804321369
Numbers from 0.500 to 0.501
0.500520813392628
0.5005912302221284
0.5006590304457654
0.5009476185730563
0.5013192031470984
List<Integer> range1 = new ArrayList<>();
List<Integer> range2 = new ArrayList<>();
for(Integer i: myArrayList)
if(i < 0.5) range1.add(i);
else range2.add(i);
Yes, use if and else
if (number > 0.498 && number < 0.499) {
firstArrayList.add(number);
}
else
{
secondArrayList.add(number);
}
And if you have 3 (or more) ranges use else if
if (number > 0.498 && number < 0.499) {
firstArrayList.add(number);
}
else if (number > 0.500 && number < 0.501)
{
secondArrayList.add(number);
}
else
{
thirdArrayList.add(number);
}

Error Incompatible types in java [closed]

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 8 years ago.
Improve this question
This is my code but it gives me this error
class MultiplyDivide {
public static void main (String args[]) {
int i = 5;
int j = 10;
System.out.println("5 is " + i);
System.out.println("10 is " + j);
int k = i/j;
System.out.println("5/10 is " + k);
k = i * j;
System.out.println("5 * 10 is " + k);
}
}
Hi,
Actually the code that you posted should not give you an error.
It will return an output like:
5 is 5
10 is 10
5/10 is 0
5 * 10 is 50
One point that can be an error from your point of view is the 5/10 equals zero. But it is a correct java behavior because you are dividing integer by integer and assign a result to an integer.
If you want to get a double-type result you need to do something like:
double k = i * 1.0 / j;
Hope this resolves your issue.

Categories