Skip to content

Tag: java

A for-loop to iterate over an enum in Java

I have an enum in Java for the cardinal and intermediate directions: How can I write a for loop that iterates through each of these enum values? Answer .values() You can call the values() method on your enum. This values() method is implicitly declared by the compiler. So it is not listed on Enum doc.

Java implementation for Min-Max Heap?

Do you know of a popular library (Apache, Google, etc, collections) which has a reliable Java implementation for a min-max heap, that is a heap which allows to peek its minimum and maximum value in O(1) and to remove an element in O(log n)? Answer From Guava: MinMaxPriorityQueue.

How to build jars from IntelliJ properly?

I have a project that contains a single module, and some dependencies. I’d like to create a jar, in a separate directory, that contains the compiled module. In addition, I’d like to have the dependencies present beside my module. No matter how I twist IntelliJ’s “build jar” proce…

Java Object Method Stack Frame Parameters

So in java, say you have a non-static method ‘bar()’ in an class ‘Foo’. Say then that you call this method like so: Now the stack frame for the call includes the integer parameter, as well as a ‘this’ parameter to be used as an internal reference to the object. What other i…

How can I get the current stack trace in Java?

How do I get the current stack trace in Java, like how in .NET you can do Environment.StackTrace? I found Thread.dumpStack() but it is not what I want – I want to get the stack trace back, not print it out. Answer You can use Thread.currentThread().getStackTrace(). That returns an array of StackTraceEle…

Can I pass parameters by reference in Java?

I’d like semantics similar to C#’s ref keyword. Answer Java is confusing because everything is passed by value. However for a parameter of reference type (i.e. not a parameter of primitive type) it is the reference itself which is passed by value, hence it appears to be pass-by-reference (and peop…