Browsing 15485 questions and answers with Jon Skeet

C#: Is it possible to: increment index by 1 and wrap back to 0 if exceed list.Count within 1 line of code?

What I'm doing right now: index++; index %= list.Count; I want to merge them into 1 quick line, something like: ++index %= list.Count; But the compiler is not allowing me...
Jon Skeet
people
quotationmark

I'd be slightly surprised if the first version worked in C or C++, but then it does surprise me quite often. The reason it doesn't work in C# is that the left-hand side of the %= operator has to be a variable, and the expression ++index... more 2/25/2017 5:31:00 PM

people

Sort a hashmap by date

This is the my json: [ {"name": "John Smith","date":"2017-02-02T23:07:09Z","id":"1223234"} {"name": "John Doe","date":"2015-07-03T21:05:10Z","id":"3242342"}, {"name": "Jane...
Jon Skeet
people
quotationmark

I have already converted the above to hashmap. I would suggest you don't do that. I suggest you convert it to a List<Person> where each Person has a name, date and ID. (If not Person then some other class.) It makes more sense... more 2/25/2017 5:25:13 PM

people

Eclipse cannot resolve package name

I have started to learn Java along with Eclipse and the book "Thinking in Java" by Bruce Eckel. I tried to add his util package (from net.mindview.util) to a project but Eclipse...
Jon Skeet
people
quotationmark

You've got one directory too far when adding the library to the build path. The library should be showing up as TIJ4Code, in the Java directory. (Or in other words, when you choose "Add class folder" you should be choosing TIJ4Code, not... more 2/25/2017 12:53:58 PM

people

Java Multi Threading, Atomic and volatile variable

I'm testing volatile variable and AtomicLong types. I have a class as such: package com.thread.methods; import java.util.concurrent.atomic.AtomicLong; public class...
Jon Skeet
people
quotationmark

You're not waiting for your threads to finish before you print out the result. Simply change your main method to join both threads after you've started them: public static void main(String[] args) throws InterruptedException { ... more 2/23/2017 3:20:48 PM

people

Log4j, meaning of Append = true / false

log4j.appender.LOGFILE.Append = true I've googled, the doc says If the append parameter is true, the file will be appended to. Otherwise, the file designated by filename will...
Jon Skeet
people
quotationmark

Yes, "the file designated by filename will be truncated" means that any data that previously existed in the file will be gone. This is a more general concept than just logging. Suppose you have a file initially containing the data... more 2/23/2017 10:29:22 AM

people

joda Period returns 0 months

I have 2 joda.DateTimes, which I use to create a Duration. The Duration is then converted to a Period, and although the two dates are 3 years apart, the Period.getDays,...
Jon Skeet
people
quotationmark

You're calling AbstractDuration.toPeriod: Converts this duration to a Period instance using the standard period type and the ISO chronology. Only precise fields in the period type will be used. Thus, only the hour, minute, second... more 2/22/2017 4:21:09 PM

people

Visual Studio C# Code colour of this

I have been fiddling with the colours of the various C# items in my new Visual Studio 2017 (Community) using the Tools-Options-Font and Colors. Everything is fine apart from the...
Jon Skeet
people
quotationmark

It's just a keyword, the same as all the others. I don't think you can tell VS to highlight it differently to other keywords. more 2/21/2017 3:53:24 PM

people

Unexpected result with Where()

I have this code, which is supposed to return a value type, applying at each step the transformations specified in steps. private static T Transformed<T>(T x, params...
Jon Skeet
people
quotationmark

The Where is lazily evaluated - you're never using the result of it, so the predicates are never being evaluated. You could force iteration by counting the results or similar: var ignored steps.Where(f => (x =... more 2/21/2017 3:36:25 PM

people

Java (Android) BufferOverflowException on putInt

I try this code : byte arr[] = ByteBuffer.allocate(2).putInt(1).array() But it fails with a BufferOverflowException. Is 1 too big to be stored in 2 bytes ? Or is my problem...
Jon Skeet
people
quotationmark

Is 1 too big to be stored in 2 bytes ? Well, an int is... putInt always writes 4 bytes. From the documentation for ByteBuffer.putInt Throws: BufferOverflowException - If there are fewer than four bytes remaining in this... more 2/21/2017 10:25:29 AM

people

C# / LINQ fastest way of comparing two lists and assigning value

I have made a code which basically compares two lists in C#. First list contains properties like this: ItemID TotalViews First list lacks values for TotalViews so I'm...
Jon Skeet
people
quotationmark

This is like jdweng's answer, but slightly simpler and it won't throw an exception for missing item IDs: var hitCountsById = HitCountItemIDS.ToDictionary(x => x.ItemID, x => x.HitCount); foreach (var item in parsedMerchantData) { ... more 2/20/2017 7:01:32 PM

people