Browsing 7239 questions and answers with Jon Skeet

Overriding Instance Method in Java with declared checked

Please help me on this, I'm unable to find out the reason why it is not getting compiled! It throws "must be caught or declared to be thrown a.eat()" error. Thanks. class...
Jon Skeet
people
quotationmark

You're calling a.eat(), so as far as the compiler is concerned, any Exception can be thrown - it only cares about the compile-time type of a. (It doesn't reason that the value of a is a reference to a Dog, and Dog.eat only throws... more 6/4/2015 5:56:42 AM

people

What is a scan: {} block in Java?

I was looking at the source code for the class String for 8u40-b25 JDK, and it contains a scan: {} block: 2557 /* Now check if there are any characters that need to be...
Jon Skeet
people
quotationmark

scan is just a label. It allow this later on: break scan; ... to allow the break statement to break out of the outer loop instead of the inner loop. See section 14.7 of the JLS for more details of labeled statements. more 6/3/2015 9:29:01 PM

people

Write float array into a binary file c#

I have a float array : float[] samples32array I need to convert it into a binary file so I can read it in matlab. Is there any way to do that?
Jon Skeet
people
quotationmark

You can use BinaryWriter to write the data to a file very easily: foreach (var value in samples32array) { writer.Write(value); } Now BinaryWriter is guaranteed to use little-endian format, so in your Matlab call, you should specify... more 6/3/2015 7:32:03 PM

people

how to import node in XML

Parent XML <order> <class/> <account> <saving/> </account> </order> I want to import node to parent xml Node: ...
Jon Skeet
people
quotationmark

All you need to do is add the element to the existing document root using the Add method, it seems: var doc = new XDocument( new XElement("order", new XElement("class"), new XElement("account", new... more 6/3/2015 6:55:52 PM

people

Async wait for two async tasks to finish

I have two function calls that are run in my code, and when both are done, the function ProcessFiles is run. Like this: byte[] file1 = doSomething(); byte[] file2 =...
Jon Skeet
people
quotationmark

Yes, you can do this easily with async/await. For example: // If you don't have async equivalents, you could use Task.Run // to start the synchronous operations in separate tasks. // An alternative would be to use... more 6/3/2015 1:25:21 PM

people

Why this private method does get executed from another class?

I Created and implemented an interface explicitly as below. public interface IA { void Print(); } public class C : IA { void IA.Print() { ...
Jon Skeet
people
quotationmark

So my question is why this private method got executed from other class? Well, it's only sort-of private. It's using explicit interface implementation - it's accessible via the interface, but only via the interface. So even within... more 6/3/2015 12:03:53 PM

people

XML XDocument There appears to be no elements

I'm trying to understand XDocument and it's various methods. I have a site map, and I'm trying to read the URLs <urlset...
Jon Skeet
people
quotationmark

How do I iterate over the loc values? The simplest way is to use the overload of Descendants which accepts an XName: foreach (var loc in siteMap.Descendants("loc")) { string value = loc.Value; ... } Currently, you're asking... more 6/3/2015 10:58:05 AM

people

Java concurrent writes from multiple threads to a single text file?

I have a multi-threaded Java 7 program (a jar file) which uses JDBC to perform work (it uses a fixed thread pool). The program works fine and it logs things as it progresses to...
Jon Skeet
people
quotationmark

Given that you've said the volume of output is low, the simplest option would probably be to just write a thread-safe writer which uses synchronization to make sure that only one thread can actually write to the file at a time. If you... more 6/3/2015 6:21:06 AM

people

What are the different ways to know which all classes and methods present in a DLL?

I was in interview and interviewer asked me the below question. How to know what classes and methods are present in DLL ? I was confused and said, "we can use tool or refactor...
Jon Skeet
people
quotationmark

I suspect the interviewer was referring to reflection. For example: var assembly = ...; // e.g. typeof(SomeType).Assembly var types = assembly.GetTypes(); var methods = types.SelectMany(type => type.GetMethods()); // etc You'd need... more 6/3/2015 6:01:34 AM

people

Optional Argument in C#, reference other argument

I'm trying to write a recursive function in C# that will take two arguments, an Array, and an index. The index should change on each recursion, but start being the length of the...
Jon Skeet
people
quotationmark

No, the default value for an optional parameter has to be a constant. You could use -1 as the default value as suggested in comments, or make it a nullable int and default it to null: public static int[] UpArray(int[] num, int? index =... more 6/2/2015 7:09:34 PM

people