Browsing 7239 questions and answers with Jon Skeet

Check if a string is startwith in an array

String[] directions = {"UP","DOWN","RIGHT","LEFT"}; String input = "DOWN 123 test"; Is there a way to check the input string is startwith one value in directions without using...
Jon Skeet
people
quotationmark

Sure - just iterate over all the directions: private static final String[] DIRECTIONS = {"UP","DOWN","RIGHT","LEFT"}; public static String getDirectionPrefix(String input) { for (String direction : DIRECTIONS) { if... more 1/25/2016 8:30:33 AM

people

Intellij IDEA "Decompiled.class file, bytecodeversion:52.0 (Java 8

I'm new to java(started to use 1 year ago). I was using Intellij IDEA community edition. Today decided install full version of it and something went wrong. It always stays in kind...
Jon Skeet
people
quotationmark

It looks like you're opening the class files within the left hand navigation window. Find the source code instead (the .java files) and open those instead - then everything should be normal. (Look at home05\Solution.java for example,... more 1/25/2016 8:24:15 AM

people

Using anonymous event in function

public static void OnAutoScrollToEndChanged(DependencyObject s, DependencyPropertyChangedEventArgs e) { /* ... */ var scrollToEndHandler = new...
Jon Skeet
people
quotationmark

If your question is basically, "Does unsubscription actually work here?" the answer is C# compiler implementation-specific, theoretically. On a practical basis, the body of the lambda expression doesn't capture any local variables, but... more 1/25/2016 7:15:48 AM

people

How is extending an interface legal in Java?

Comparable is defined as an interface. In that case, why is Key K extends Comparable<K> legal in the following case, and why isn't K implements Comparable<K> used...
Jon Skeet
people
quotationmark

Well, K could be an interface extending Comparable<K> instead of a class implementing it... in which case extends would be more suitable. As the declaration of Entry doesn't know whether K will be an interface type or a class type,... more 1/25/2016 6:48:39 AM

people

unusual behaviour of HashCode in java

I am stuck around while studying hashCode() in java. I wrote a sample code PFB snippet public class Shared { String type; Date date; public Shared(String type) { ...
Jon Skeet
people
quotationmark

I am really confused how this is possible even after changing the value contained within the key hashCode is same. it's literally breaking the immutable key concept. Nope, it's fine. You're not overriding equals or hashCode, so it's... more 1/24/2016 8:15:27 PM

people

C# Unit testing. Verify list count

I wanted to have a test case where I can verify a list of IRule count increases as new IRule item added. The method I am testing is AddRule. I wanted to keep the 'rules' property...
Jon Skeet
people
quotationmark

There are various options here: Expose rules publicly but safely, e.g. via a ReadOnlyCollection<T> wrapper or as an IEnumerable<IRule> via return rules.Select(r => r); to avoid the actual list being exposed via casting.... more 1/22/2016 7:53:15 AM

people

Issue with retaining loop variable value

I have an error in this piece of code where I have declared public class variable mCountryCode as String. for (mCountryCode : isoCountryCodes) { locale = new Locale("",...
Jon Skeet
people
quotationmark

Yup, the enhanced for statement just doesn't work like that. It always declares a new variable. You can use: for (String tmp : isoCountryCodes) { mCountryCode = tmp; ... } ... although frankly that's a pretty odd thing to do.... more 1/21/2016 7:05:04 PM

people

How to Define Extension Method for ICollection<T> where T : IMyInterface without Specifying T in the Method Definition

Background: I want to hook in business rules at the point that DTOs are being mapped to entities. I figured encapsulating the mapping into an extension method would be a good...
Jon Skeet
people
quotationmark

You effectively need a method with a signature of public static ICollection<TEntity> MapToCollection<TEntity, TEntityDto>( this ICollection<TEntityDto> dtos) where TEntityDto : IEntityDto ... but that would... more 1/21/2016 5:45:35 PM

people

Explanation regarding skipping of alternate lines while reading from a file?

I just want to know, have I interpreted the code in right manner? The file Bharath.txt has the following content: Hello ! B . We created your file succesfully . We have...
Jon Skeet
people
quotationmark

But , when I do not consider storing " input.nextLine()" in a String , it begins to skip line alternatively . Is it , that , it is taking even the ENTER from keyboard as input ? Well no, it's just reading the next line every time you... more 1/21/2016 3:49:12 PM

people

Overload is the only way to add different parameter to the same function?

I have 2 functions that does the same exact operation; since the underlying API has an overloaded fuction that accept either a string or an int. Since I am using this function, I...
Jon Skeet
people
quotationmark

You could use dynamic typing here: // I don't recommend you do this - see later public void TakeTwo(dynamic value1, dynamic value2) { baseAPI.GetValues(value1, value2); } The overload resolution for the call to GetValues will then... more 1/21/2016 7:03:51 AM

people