Browsing 7239 questions and answers with Jon Skeet

How to avoid individual casting to byte during initialization of byte array in Java

I am using a byte array to store a text file outside of the filesystem. It looks like this: private static final byte[] CDRIVES = new byte[] { (byte)0xe0, 0x4f,...
Jon Skeet
people
quotationmark

Well one simple approach would be to use base64 - but perform the conversion on class initialization, so you only take the performance hit once: private static final byte[] CDRIVES = Base64.decode("YOURBASE64HERE"); Or if it's genuinely... more 7/30/2015 7:32:10 AM

people

How to take one row of two dimension array?

I want to have a bicubic interpolation by using C# , but I can't deal with array. double cubicInterpolate(double[] p , double x) { return p[1] + 0.5 * x * (p[2] -...
Jon Skeet
people
quotationmark

You can't, basically. You've got a rectangular array, which is a single block of memory. You could write your own wrapper around that, e.g. public RectangularArrayRow<T> : IList<T> { private readonly int row; private... more 7/30/2015 5:50:35 AM

people

Parse xml by Xdocument

I have a simple xml file: <?xml version="1.0" encoding="utf-8" ?> <Parameters> <Valid> <SetSensorParameter> ...
Jon Skeet
people
quotationmark

Rather than using Nodes, it would be simpler to use Elements, so that you can then use the Attribute method to retrieve each attribute: var parameters = doc.Root .Element("Valid") ... more 7/29/2015 3:25:31 PM

people

Java Array initialization with type casting

The following code makes me confused: Object[] arr1 = new String[]{"a", "b", "c"}; Object[] arr2 = {"a", "b", "c"}; String[] a = (String[]) arr1; // ok String[] b = (String[])...
Jon Skeet
people
quotationmark

arr2 is exactly a String[] No, it isn't - it's an Object[], as you said - your line is equivalent to: Object[] arr2 = new Object[] {"a", "b", "c"}; It's an Object[] which happens to have elements which are all string references at... more 7/29/2015 12:46:26 PM

people

displaying the lowest and highest of 2d array

I am trying to display the lowest and the highest value. The highest value is correct but the smallest is always giving me 0 can you help me. package openlink.domain; import...
Jon Skeet
people
quotationmark

Well, you've initialized small as 0 to start with... so you're only going to update it if there are negative numbers. Likewise you've initialized large as 0, so you'll only update that if there are positive numbers. Enter all-negative... more 7/29/2015 6:18:20 AM

people

Write to properties file inside a java Package

How to write to properties file in a java package using java class in another package. Here is the code for writing properties file String filePath1 =...
Jon Skeet
people
quotationmark

There are three problems here, which are related. Basically, you're assuming that get because you can read from a resource, you can write to a file in the same folder structure, relative to the current directory. That's a flawed assumption... more 7/29/2015 6:02:09 AM

people

Why do we assign child class object to parent class reference variable?

I have the following code. public class Parent { public void Print() { Console.WriteLine ("Parent Method"); } } public class Child : Parent { public new...
Jon Skeet
people
quotationmark

It's because you've redeclared the Print method in Child. So at compile time, P.Print() resolves to Parent.Print, but C.Print() resolves to Child.Print(). If you had a virtual method which was overridden in Child instead, they'd both print... more 7/28/2015 6:49:46 PM

people

What is C# 6.0 #pragma disable warnings feature?

The list of C# 6.0 final features contains a feature called #pragma listed as "Added" and the example is to disable warnings. However this feature did exist in C# before 6.0. What...
Jon Skeet
people
quotationmark

Previously, you had to specify the warning number. So to disable CS0501, you'd use #pragma warning disable 0501 Now, you can use #pragma warning disable CS0501 ... which is incredibly important when you've got Roslyn Code Analyzers... more 7/28/2015 4:53:47 PM

people

Create generic extension method with enumerations

I am fairly new to C# and I am trying to create a generic extension method that that takes any enumeration as the parameter. Based on the enumeration I want to retrieve an...
Jon Skeet
people
quotationmark

How do I make this a generic extension method? You can't, at the moment... because C# doesn't allow you to constrain a type parameter to derive from System.Enum. What you want is: public static string GetIdForEnum<T>(T value)... more 7/28/2015 4:48:37 PM

people

String.valueOf() behavior with char[] in List<char[]>

I was working with char[] and Collection with the below code :- char[] ch = { 'a', 'b', 'c' }; List<char[]> chList = new ArrayList<char[]>(); ...
Jon Skeet
people
quotationmark

You're seeing the difference of calling different overloads of String.valueOf. Here's a simpler example: public class Test { public static void main(String[] args) { char[] chars = { 'a', 'b', 'c' }; ... more 7/28/2015 12:49:04 PM

people