Browsing 7239 questions and answers with Jon Skeet

Linq Indexed Select

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) }); ...
Jon Skeet
people
quotationmark

What is index in the above linq query ? It's the index of the element being processed. So the first element (5) will have an index of 0, the second element (4) will have an index of 1 etc. How it brings the index from the array... more 11/8/2013 7:02:00 AM

people

Convert delegate to System.Action

Introduction In my WP8 C#/XAML project I'm using events to notify my view that some async process is done. I have two types of delegates. I'm creating events from those...
Jon Skeet
people
quotationmark

To answer this line: Why isn't it convertible? They're different delegate types, and there's no reference conversion between different delegate types (other than in generic delegate types using generic variance). You can create an... more 11/8/2013 6:50:53 AM

people

Prevent out of order execution in java

Essentially I have the following two statements: Project project = projectDao.createProject(...); projectDao.deleteInProgressEntry(...); I want to make sure that the project...
Jon Skeet
people
quotationmark

The statements are completely independent of each other, so I am worried that the compiler may not respect the ordering in the code. No, it will. It's possible that other threads may observe the results of the operations out of order... more 11/7/2013 11:07:00 PM

people

Generic type inference doesnt take polymorphism into account

Hi here is my example let's say class A { } class B : A { } void test<T>(T clazz) { Console.WriteLine("clazz type = {0} T type = {1}", ...
Jon Skeet
people
quotationmark

The result is clazz= B T= A ????? why inference generic type doesn't take into account polymorphism ? Type inference is performed at compile time. The compile time type of the variable b is A, so the compiler infers that you... more 11/7/2013 10:52:10 PM

people

NullPointerException at int array in Java Project

I am creating a method which will take int n as a parameter, convert a previously initialized String into an integer array, add 1 to each index n times, create a new char array...
Jon Skeet
people
quotationmark

Look at your code: int[] a = null; char[] b = null; int r = 0; for (int i = 0; i <= text.length(); i++) { a[i] = text.charAt(i); } a is null - you've explicitly set it to null. So a[i] = text.charAt(i); will fail with a... more 11/7/2013 9:37:16 PM

people

Scanner only reads file name and nothing else

I'm trying to implement a rudimentary lexer. I'm stuck on the file parsing at the moment. public ArrayList<Token> ParseFile () { int lineIndex = 0; Scanner scanner...
Jon Skeet
people
quotationmark

You've misunderstood the API for Scanner. From the docs for the Scanner(String) constructor: Constructs a new Scanner that produces values scanned from the specified string. Parameters: source - A string to scan It's not a... more 11/7/2013 9:04:25 PM

people

C# random if statement only output = 1

I'm trying to make my program output either 1 or -1 this is my code so far and its only output is -1. It's not random at all. Random rnd = new Random(); int L = rnd.Next(0,...
Jon Skeet
people
quotationmark

The second argument Random.Next(int, int) gives an exclusive upper bound. So you're saying you want an integer greater than or equal to 0, and less than 1. That doesn't give a lot of scope for numbers other than 0 :) From the... more 11/7/2013 8:53:52 PM

people

Convert String to Date error parsing

I have looked at many examples and can still not find an answer to match my problem. I have now been stuck for an hour and need help. I have many strings that are formated like...
Jon Skeet
people
quotationmark

(When I originally answered the question, the format string used hh - it has since been changed.) You're using hh for the hours part, which is a 12-hour format - but providing a string which uses "18". You want HH. Additionally, I'd... more 11/7/2013 7:02:28 PM

people

How can i avoid java.lang.NullPointerException error

I have some problem with my code. First of all, here are my codes. public class Zoo { public int j=0; public Animal[] park; // Exercise 9 public Zoo() { Animal[] park = new...
Jon Skeet
people
quotationmark

This is the problem: public Animal[] park; public Zoo() { Animal[] park = new Animal[10]; } You're declaring an instance variable called park - but then in the constructor, instead of assigning a value to that instance variable,... more 11/7/2013 11:58:16 AM

people

wrong time calculating in Java

when I want to sum two dates in java it does not work: System.out.println(date + " <---- date"); System.out.println(time + " <---- time"); System.out.println(new...
Jon Skeet
people
quotationmark

Fundamentally, you've got problems with time zones here. The fact that you're using a java.util.Date to represent a time of day is messing you up to start with. Your time of 11:51:14 CET is actually 10:51:14 UTC, so when you add the result... more 11/7/2013 11:50:12 AM

people