Browsing 7239 questions and answers with Jon Skeet

How to pad an integer number with leading zeros?

I have strings that are being converted that need to be four characters long. The values are coming in as anywhere between 0 and 4 characters long. I need to pad the strings with...
Jon Skeet
people
quotationmark

You're not doing anything with the result of PadLeft - but you don't need to do that anyway. You can just specify the number of digits (and format) in the ToString call: string result = temCode.ToString("d4"); more 2/10/2014 9:41:06 AM

people

Recursion and anonymous method in C#

Good day. I have method for recursive traverse TreeNode in TreeView: public void ShowTree(TreeView tree) { foreach (TreeNode node in tree.Nodes) { ...
Jon Skeet
people
quotationmark

If you are going to split this out, I would actually split the recursion part from the "what you do with each node" part. So something like this: public static void ApplyRecursively<T>(this IEnumerable<T> source, ... more 2/10/2014 7:44:09 AM

people

Lambda failed to update value?

I have a list and I'm trying to set one boolean field status to true if the name field is not haha. Below is what I thought it suppose to work but apparently it's...
Jon Skeet
people
quotationmark

That certainly wouldn't do what you wanted it to. ForEach doesn't return anything. It executes the delegate you provide against each item in the list, and that's all. Personally I'd just use Where with a foreach loop: foreach (var item... more 2/10/2014 7:23:59 AM

people

Transfer NULL to the constructor

I can not understand why the constructor is executed with the parameter Double[]? using System.Collections.Generic; using System.Linq; using System.Text; namespace...
Jon Skeet
people
quotationmark

But I can not understand why no object? It's also a reference type? Yes, both double[] and object are reference types, so null is implicitly convertible to both of them. However, member overloading generally favours more specific... more 2/9/2014 8:52:48 PM

people

trying to add two BigInterger Numbers together in a class Constructor and gettiing stackoverflow error

trying to add two BigInterger Numbers together in a class Constructor and gettiing stackoverflow error. import java.math.BigInteger; public class BigNumber implements Cloneable...
Jon Skeet
people
quotationmark

The stack overflow isn't in your constructor. It's when your main method calls add on your BigNumber. This is the first problem: public BigNumber add(BigNumber other) throws CloneNotSupportedException { return ((BigNumber)... more 2/9/2014 7:47:32 PM

people

Rearrangement of objects in arraylist

public void go() { ArrayList<String> fruits = new ArrayList<>(); fruits.add("pear"); fruits.add("pineapple"); fruits.add("oranges"); ...
Jon Skeet
people
quotationmark

I would split the list into two: items that contain apple, and items that don't. Then sort the apple-containing list, and then append the non-apple-containing list. List<String> apples = new ArrayList<>(); List<String>... more 2/9/2014 4:13:46 PM

people

Why isn't it an error?

The following program is a recursive program to find the maximum and minimum of an array.(I think! Please tell me if it is not a valid recursive program. Though there are easier...
Jon Skeet
people
quotationmark

The return type is clearly an integer array (as specified in the method definition), but I have not assigned the returned data to any integer array, but the program still works. Yes, because it's simply not an error to ignore the... more 2/9/2014 3:36:21 PM

people

Can an object with different generic types be added to a single container

I need to have an array that contains a set of "generics" objects, where each element in the array might have the same generic type, or be different. The only way I could solve...
Jon Skeet
people
quotationmark

No, because you're trying to make the compile-time type of p depend on the execution-time type of the value within the collection. Imagine you're writing a compiler, and you're provided with this code: public void Foo(MagicCollection... more 2/8/2014 4:12:20 PM

people

Access to protected . What differencies which link use(parent or child)?

Please help me with this issue: consider this code import java.util.Observable; import java.util.Observer; public class MyObservable extends Observable { int k; public...
Jon Skeet
people
quotationmark

It's not really a static/non-static difference - it's a difference in the compile-time type of the reference you're trying to call the method on. In the instance method, you're implicitly calling it on this, which has a compile-time type... more 2/8/2014 1:56:48 PM

people

Java: Importing resources, the use of "*"?

I am currently working on a tutorial that uses the following imports: //importing Resources import java.awt.*; import java.awt.event.ItemEvent; import...
Jon Skeet
people
quotationmark

My question is: does java.awt.* import everything from awt? It imports all the classes in java.awt, but that's all. And if so why is this necessary? import java.awt.event.ItemEvent; Because that's not in java.awt - it's in... more 2/8/2014 1:53:33 PM

people