This question already has answers here:
Comparing arrays in JUnit assertions, concise built-in way?
(8 answers)
Closed 8 years ago.
I've written a method that returns an int[]. I want to create a test that ensures that when I run my method, passing, say, 10 to the method, that it returns an int[]{0,1,3,5,7}.
How can I do this? Maybe I'm just missing the proper syntax or should be using a different assert method...?
If you are using JUnit4, you could use the org.junit.Assert class:
Assert.assertArrayEquals(new int[]{0,1,3,5,7}, someMethod(10));
Related
This question already has answers here:
Multiple lambda method references
(3 answers)
Closed 5 years ago.
list.stream().forEach(e -> method(e)) can be converted to list.stream().forEach(this::method)
Similarly can we convert list.stream().forEach(e -> { method1(e); method2(e);}); using method references expressions. Big apologies if you don't understand question. I am using mobile app first time.
No you cannot.
The point of Method references in Java is to abstract (syntaxically) a lambda expression. Since forEach consumes a function that takes 1 element of type specified by the parent stream, there is no syntax sugar for double application using method references.
Even I'm not sure that this answer is wanted by you,
How about changing the method to static one in that class?
This question already has answers here:
"+" operator for Java-classes
(5 answers)
Closed 5 years ago.
I'm trying to make a java library with a bunch of extra classes, and I was adding one for Imaginary numbers. Is there any way in java to make a custom class that is affected by mathematics operations
for example
Imaginary(10) * Imaginary(50) = "500i"
No, it is not possible to define custom arithmetic operators that overload default operators, but you can create methods like Imaginary.mutiply(Imaginary i).
You cannot overload operators in Java. See this previous answer to a similar question: https://stackoverflow.com/a/5883909/1701316
Your class will need to implement its operations as methods. If you'd like, since any character is allowed in a method name, you can name them with the typical operators, but they'll still need to be called with dot-notation: Imaginary(10).*(Imaginary(50))
This question already has answers here:
What's the difference between a method and a function?
(41 answers)
Closed 7 years ago.
I was wondering What is the different between Method and Function in JAVA?
it the same, function or methods are just different ways to call it, but in my long experience with java i commonly hear function from my fellow developers the last time i heard anyone say 'method' was back when i still used C++
This question already has answers here:
How to handle java variable length arguments in clojure?
(3 answers)
Closed 8 years ago.
Code in clojure:
(import '(java.nio.file Files))
(Files/createTempDirectory "Test")
There is error:
CompilerException java.lang.IllegalArgumentException: No matching method: createTempDirectory, compiling:xxxx
But in java's doc http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempDirectory(java.lang.String,%20java.nio.file.attribute.FileAttribute...)
There is an String parameter for createTempDirectory, I'm using java 1.7.0
Try this code:
(java.nio.file.Files/createTempDirectory "Test"
(into-array java.nio.file.attribute.FileAttribute []))
As #ymonad mentioned, you cannot omit the variable argument when calling java method with variable arguments. If you don't want to specify the FileAttribute, just pass the empty array of the type.
This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 9 years ago.
Working in Java, what is the difference between using
Object[] variableName;
and using:
Object variableName[];
Does it have the exact same effect on compilation and run? Or is there a difference?
Both statements are entirely equivalent.
The statements will compile to the same code, BUT if you write
Type[] name instead of Type name[] the code becomes more readable, because you always can see the type (Array or Not-Array) in front of the variable name. (In fact this is some kind of my ppersonal meaning)
From Java language specification (for Java 7) :
The [] may appear as part of the type at the beginning of the
declaration, or as part of the declarator for a particular variable,
or both.
So yes, they are both equivalent and you can even mix the two styles in the same declaration (although the specification gives a healthy reminder to us that that tends to get ugly and confusing).