Browsing 7239 questions and answers with Jon Skeet

Of which elements does Generics allow parameterization by type?

This is a question from some tests. Question is "Of which elements does Generics allow parameterization by type?" and 5 variants of answers: Classes Structs Methods ...
Jon Skeet
people
quotationmark

Property is parametrized by type, correct (the same with Events)? No, it's not. There's no type parameter on the property itself - you've got it on the class. Compare that with your method example: class GenericList<T> { ... more 2/26/2015 1:29:08 PM

people

getCause() function of Throwable class (in Java) is not working as expected

I am getting null while calling the getCause function of Throwable. package com.salman.demo; public class MyClass { public static void main(String[] args) { // TODO...
Jon Skeet
people
quotationmark

The ValidationException you've thrown will be th in your calling code... there's no cause for that ValidationException. The point of getCause() is for one exception to be able to refer to another one as an underlying cause - but there's... more 2/26/2015 1:20:00 PM

people

What is the return value of async Task?

Imagine I have this async method: public async Task Foo(object fooObj) { //do foo's stuff. //maybe set a fooable static global variable //or write some data to...
Jon Skeet
people
quotationmark

Ok, so: this first code block returns a Task, but it isn't actually returned anywhere. Indeed. You can think of it as being like Task<void> - if that were valid. It's used to indicate when the asynchronous method has... more 2/26/2015 11:27:35 AM

people

How to handle BitSet type table with the BitSet methods?

I have to create a table with two type BitSet dimensions (9x9). I fulfills this table with bit values 1 to 9. I wish to withdraw a value of a particular case (exemple 5), but .set...
Jon Skeet
people
quotationmark

You've created a 9x9 array of BitSet references, but set every element value to the same reference - there's only a single BitSet object involved. This is just a more complex version of this: StringBuilder builder = new... more 2/26/2015 11:24:00 AM

people

Is there a way to "expand" a list of objects into a bigger list with stream API?

Consider this example: I have a list of RangeSet that contains, for instance, timestamps. I want to get the total duration of ranges using java8 streams instead of the imperative...
Jon Skeet
people
quotationmark

It looks like you're just looking for Stream.flatMap, e.g. long totalTime = list.stream() .flatMap(rangeset -> rangeset.asRanges().stream()) .map(range -> range.upperEndpoint() - range.lowerEndpoint()) .reduce(0, (total,... more 2/26/2015 11:10:45 AM

people

What's wrong with my switch statement?

I'm trying to make a switch statement in java but I get this error even though my syntax is correct: Syntax error on token "{", SwitchLabels expected after this tokenI know that I...
Jon Skeet
people
quotationmark

Two immediate problems with the first two lines: switch(true){ You can't switch on boolean values input = scan.next(); You've got this line immediately within the switch statement - it needs to be within a case statement. I suspect... more 2/26/2015 10:55:45 AM

people

convert JSON string to array mono

I'm trying to convert a string with JSON data to an array. I'm using NewtonSoft.JSON The following line of code holds my JSON data in a string: string jsonString =...
Jon Skeet
people
quotationmark

You just need to call DeserializeObject providing the array type instead of the single class type. Here's a short but complete program demonstrating this... I've adjusted your JSON and class to match each other. using System; using... more 2/26/2015 10:31:10 AM

people

How to change from which argument is the type inferred in generic methods?

Suppose there's a static method in my Utils class, that sets the value of a property. public static SetPropertyValue<TDest, TVal> (Expression<Func<TDest,...
Jon Skeet
people
quotationmark

No, you can't affect type inference that way - and if it did infer TVal as short, your code wouldn't compile, because there's no implicit conversion from int to short. Surely the best solution is just to avoid using two different types to... more 2/26/2015 9:16:14 AM

people

System.currentTimeMillis() not displaying time in UTC , instead displaying current timezone time

I have a requirement of getting UTC time , hence in many places iam using below code for calcualting utc time. System.currentTimeMillis() ; Iam in IST - (GMT+5:30) ,...
Jon Skeet
people
quotationmark

System.currentTimeMillis() just returns a long - that's not in any sort of date format. If you're actually using: Date date = new Date(System.currentTimeMillis()); System.out.println(date); then you're just seeing the result of... more 2/26/2015 7:05:58 AM

people

Do blocking of global iterator make sense?

I need to do some stuff what request reading of each elements of arrey in threads. Each thread just need read element [i] and do i++. Do i need to lock i++? I read somewhere...
Jon Skeet
people
quotationmark

The important point here is that you have a shared variable, and the ++ operator does a read/modify/write operation. That operation is not atomic, even though each individual part of it is. So you could easily have: Thread 1 ... more 2/26/2015 7:00:06 AM

people