Browsing 7239 questions and answers with Jon Skeet

Complexity of searching in a list and in a dictionary

Let's say I have a class: class C { public int uniqueField; public int otherField; } This is very simplified version of the actual problem. I want to store multiple...
Jon Skeet
people
quotationmark

Assuming you've got quite a lot of instances, it's likely to be much faster to find the item in the dictionary. Basically a Dictionary<,> is a hash table, with O(1) lookup other than due to collisions. Now if the collection is... more 7/24/2015 11:25:29 AM

people

Unable to access StorageFolder.TryGetItemAsync() method

I'm working on Windows Phone 8.1 RT and I want to check if a particular folder/file exists or not in a StorageFolder. The documentation is defined here, but I'm surprised to find...
Jon Skeet
people
quotationmark

No, that method isn't supported on Windows Phone 8.1, apparently. The key is to look at the "Requirements (Windows 8.x and Windows Phone 8.x)" section at the bottom of the docs, which shows: Minimum supported client Windows... more 7/24/2015 11:16:53 AM

people

Multiline output in Netbeans?

I'm working on a program, with a GUI, that will need to output multiple lines to some kind of TextArea. I tried doing this with a JTextArea, but it turns out that when setting a...
Jon Skeet
people
quotationmark

Just use the append method instead of setText. Everything's behaving exactly as I'd expect it to - I would have been really surprised if setText had appended. If you don't want to use append for some reason, you can call getText and... more 7/24/2015 8:04:09 AM

people

How to construct ZonedDateTime from an Instant and a time string?

Given an object of Instant, a time string representing the time at a specific ZoneId, How to construct a ZonedDateTime object with the date part (year, month, day) from the...
Jon Skeet
people
quotationmark

You'll want to parse the time string to a LocalTime first, then you can adjust a ZonedDateTime from the Instant with the zone, and then apply the time. For example: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm",... more 7/23/2015 4:54:51 PM

people

Strange behavior of Enumerator.MoveNext()

Could someone explain why this code is running in infinity loop? Why MoveNext() return true always? var x = new { TempList = new List<int> { 1, 3, 6, 9 }.GetEnumerator()...
Jon Skeet
people
quotationmark

List<T>.GetEnumerator() returns a mutable value type (List<T>.Enumerator). You're storing that value in the anonymous type. Now, let's have a look at what this does: while (x.TempList.MoveNext()) { // Ignore... more 7/23/2015 3:08:28 PM

people

Selecting items in an ordered list after a certain entry

I have an ordered list of objects. I can easily find an item in the list by using the following code: purchaseOrders.FirstOrDefault(x => x.OurRef.Equals(lastPurchaseOrder,...
Jon Skeet
people
quotationmark

It sounds like you want SkipWhile: var orders = purchaseOrders.SkipWhile(x => !x.OurRef.Equals(...)); Once the iterator has stopped skipping, it doesn't evaluate the predicate for later entries. Note that that code will include the... more 7/23/2015 2:45:21 PM

people

Http status 404 /the requested resource is not available

I am new to java, I just tried to read initialization parameters from Deployment Descriptor file (web.xml), But got above error? My web.xml and java file coding coding in snap...
Jon Skeet
people
quotationmark

There are two problems I can see at the moment... Servlet init parameters You currently have: //defining param1 param1 value1 That's not how you define the parameter. You should specify a param-name element containing the... more 7/23/2015 8:03:37 AM

people

Which is considered better to get the individual digits of a number? Divide modulus or string charAt?

Approach 1: Repeated Division-modulus operations. long num = 123456789; int count = 0; while(num > 0) { int digit = num % 10; if(digit == 1) count ++; num /=...
Jon Skeet
people
quotationmark

In this particular case, I'd say it's reasonable to use a string representation. After all, the count you're finding is less inherently about its numeric value than its decimal string representation - you'd get a different value if you... more 7/23/2015 6:15:40 AM

people

How to get a protobuf repeated field builder in Java?

I want to convert an object of another format into a protobuf, knowing the protobuf's Descriptors. It's easy to do for regular fields or even a nested field. But, I'm running...
Jon Skeet
people
quotationmark

You don't get a builder for the repeated field itself - you call Builder.addRepeatedField(field, value) etc. To get a builder for the type of the repeated field, you can use: Builder builder = bld.newBuilderForField(field) If you want... more 7/22/2015 10:06:18 PM

people

Option Strict On does not allow narrowing in implicit type conversions between method and delegate

Error: Option Strict On does not allow narrowing in implicit type conversions between method 'context_beginRequest<ByVal sender As Object, ByVal e As...
Jon Skeet
people
quotationmark

That's because HttpApplication.BeginRequest is just of type EventHandler... so your second parameter should be of type EventArgs, not WindowsAuthenticationEventArgs. more 7/22/2015 7:50:35 PM

people