Why didn't this give an error? [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I spent a long time on this. Why didn't it give some sort of error? I don't understand:
This java code snipette (I will put comments for code I omitted just for the example):
String[] sortOrder = { "Ascending","Descending" }
for ( String order : sortOrder ) {
if (sortOrder.equals("Ascending")) {
// code here to sort array in ascending
} else if ( sortOrder.equals( "Descending") ) {
// code here to sort descending
}
}

In the javadocs it states:
true if this object is the same as the obj argument; false otherwise.
The equals method is returning false instead of an error because your array is not equal to the
string value of "Ascending" and "Descending"

Related

How to get postfix expression from parse tree? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
public String auxToPostfixString(Node root) {
String result = "";
if (root == null) {
return "";
}
result += auxToPostfixString(root.getLeft());
result += auxToPostfixString(root.getRight());
result += root.getExp();
return result;
}
I used this code for that, and it should return 342*+8+ but it returns 34+2*8+ (the original expr is 3+4*2+8) What's wrong about this?
Sorry for bad English
it should return 342*+8+ but it returns 34+2*8+ (the original expr is 3+4*2+8)
the problem may come from the original parsed tree : was the priority of multiplication against addition well applied ? Make sure that the tree is not, in fact, equivalent to ((3+4)*2+8).

list.stream().collect(Collectors.toList()); returns empty list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
choices is a List of two elements
but choices.stream().collect(Collectors.toList()); returns an empty list
Would anyone know why?
//returns poll with list of choices
public Poll accessPoll(String pollId) {
return pollRepository.findById(pollId).orElseThrow(
() -> new IllegalStateException(String.format("No poll found for the ID: %s.", upperCasePollId)));
}
List<Choice> choices = pollManager.accessPoll(pollId).getChoices(); //returns list of choices
List<Choice> choices1 = pollManager.accessPoll(pollId).getChoices()
.stream().collect(Collectors.toList()); //returns empty list
Look carefully at your screenshots. Your method getChoices() returns not a regular list but IndirectList which extends not a regular Collection but a Vector and that is why streams don't work as expected. This is a known bug in EclipseLink,
you can read about it more here and here.
To overcome this behaviour, you can try to update your EclipseLink version up to 2.6.0, or you may try to wrap it with a new collection, like new ArrayList<>()

When does Common subexpression elimination apply? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
for (Pair p : pairs) {
double f = foo(p)
...
}
foo() performs a simple mathematical calculation as follows:
double foo(Pair p) {
return Math.cos(p.x) + Math.sin(p.y);
}
If all the items in pairs are the same, can the Java compiler optimise this using Common subexpression elimination? Or is there another optimisation that occurs? I am asking since I have found a significant time reduction when more values in pairs are the same.

Unreachable If Statements with Remove Function for Singly Linked Lists [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to create my own singly linked list in Java and am running into trouble when writing my remove function (which will take the desired node out of the list). 'hasNext' is a boolean that returns true if there is a node after something. The error I'm getting is that the if statements I have are unreachable. Any idea how to go about fixing this?
Change
if (hasNext == false) // If you're removing the final value
to
if (hasNext() == false) // If you're removing the final value
Since you have no code that sets hasNext to false you can't get into that if block.

How would you display each element of an array that is returned from another method in Java? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
For example, I have my main method as well as the method returnOdds(original), that returns the integer array "odd"
How would I print the elements of this array in the main method? With a for loop?
A for loop would work. If you don't care about the format, though, a simpler solution might be to use Arrays.toString, which will convert in the form "[elem1, elem2, ..., elemn]".
"With a for loop?"
answer : "yes"

Categories