[Solved] HashMap of ArrayList as the value initialization

The List<Pack> in your HashMaps are never defined. They are of the interface list but nowhere you identify what kind of List they are. You have to follow a structure as follows: Map<long, List<Pack>> map = new HashMap<>() // java 7 syntax List<Pack> packets = new ArrayList<>(); packets.add(pack); map.put(msg.getAck(), packets); When adding an element to … Read more

[Solved] Stack implementation java

pop() is not working because you are using different objects for push and pop. You don’t need to define another class for push and pop, they are operation add those function inside Stack class. class Stack { … // members and constructor public void push(){..} public void pop(){..} public void show(){..} } And create an … Read more

[Solved] Java Mysterious NullPointerException

If the first line in the method isn’t executed, then the method simply doesn’t get called. There is a chance, that it is actually executed and System.out has been redirected so that it doesn’t print to the console but to somewhere else. Take a debugger, set a breakpoint on all lines of code that call … Read more

[Solved] Using `Set` Argument to Scala Code in Java

Demonstrating what the commenter said: scala> import collection.JavaConverters._ import collection.JavaConverters._ scala> val js = (1 to 10).toSet.asJava js: java.util.Set[Int] = [5, 10, 1, 6, 9, 2, 7, 3, 8, 4] scala> def f(is: collection.mutable.Set[Int]) = is.size f: (is: scala.collection.mutable.Set[Int])Int scala> def g(js: java.util.Set[Int]) = f(js.asScala) g: (js: java.util.Set[Int])Int scala> g(js) res0: Int = 10 3 … Read more

[Solved] Java input from the user and output as a list at the end

The best way I would see to do this would be to use Vectors. This is not like arrays where you need a defined size in order to proceed. Vectors can grow and shrink. See [http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html] for more details. Basing this off the code you have already given I would recommend the following: import java.util.*; … Read more

[Solved] How to fix ClassCastException in enhanced for-loop? [closed]

The only way the problem can exist given the posted code, if the reported exception/line is correct, is when “Somerecords” is not really creating a List<Object[]> object – but a List containing non-Object[] elements. One reason could be the method in question is typed to return a non-generic List, filled with double values. Java will … Read more

[Solved] Nested Java methods [closed]

You cannot nest method declarations. However, you can simply define them separately and call one from the other. That is the purpose of functions. public static boolean divides(int num, int denom) { if (num%denom==0) return true; else return false; } public static boolean isLeapYear(int year) { return divided(x, y); // not sure what you are … Read more

[Solved] non-static method cannot be referenced from a **static context**. What is static content here? [duplicate]

Your main-method is the static context, and you are trying to call the non-static method display() from it. That doesn’t work even if it is in the same class. To have the disply method non-static you have to do this. public static void main(String[] args) throws FileNotFoundException{ ReverseLL r = new ReverseLL(); r.display(head); } public … Read more