Browsing 7239 questions and answers with Jon Skeet

How to write an ArrayList to a text file?

I have an ArrayList of strings and a text file called actors. I need to write all the elements in the ArrayList to a .txt file. Here is my code so far. void WriteArrayList() ...
Jon Skeet
people
quotationmark

If you really must use ArrayList, and assuming you're using .NET 3.5 or higher, you can use the LINQ Cast method to convert it to a sequence of strings: // .NET 4 and higher File.WriteAllLines(@"c:\actors.txt",... more 10/21/2013 5:49:15 AM

people

Need help on this for loop

For my Java practice midterm, we need to write the exact output of a line of code, in this case, a for loop. Here's the code: for(int first = 3; first > 0; first--) ...
Jon Skeet
people
quotationmark

You're decrementing first twice: once each time the outer loop iterates, and once each time the inner loop starts iterating. So after printing 2 5 it hits the end of the inner loop, and hits the first-- from the outer loop. Then as it... more 10/20/2013 7:31:51 PM

people

How to create a Method that creates a new List and takes additional parameters

Is there a way to create a Method that creates a new instance of a List as a Method Parameterand does something with the new List? private void...
Jon Skeet
people
quotationmark

It sounds like you should be returning the list reference, rather than using it as a parameter - otherwise the fact that you're assigning a different value to it in the first statement of the method body makes it pointless: private... more 10/20/2013 3:06:19 PM

people

Can initializer block throw exception?

I am using BufferedReader in a class to read from a file. I am trying to initialize this in initializer block. class ReadFromFile { BufferedReader br; { br =...
Jon Skeet
people
quotationmark

An initializer block can only throw unchecked exceptions, or checked exceptions which are declared to be thrown by all constructors. (This includes exceptions which are subclasses of those which are declared.) You can't throw a checked... more 10/20/2013 2:38:33 PM

people

Using a class within an enhanced for loop

I've written a class that implements Collection and Iterable, and I am attempting to run it through an enhanced for-loop now. You can find it here: LinkedQueue The point of the...
Jon Skeet
people
quotationmark

Your implementation is failing because when you (implicitly) call iterator(), your itr variable still refers to just the "null value" node. You only set it to any other value in the next() method, which isn't called until later. However,... more 10/19/2013 9:37:08 PM

people

Text property of derived TreeNode?

I have some custom TreenodeTypes I want to fill with custom values. They look like this: public class QuestionNode : TreeNode { public string Question { get; set; } ...
Jon Skeet
people
quotationmark

I understand that the base-Property is used instead of "my" property. Is there a way to change that? Well not really. The problem is that it's not a virtual property, and you've just introduced a new property with the same name - it's... more 10/19/2013 9:30:25 PM

people

Why am I still on the Main Thread when I specified ConfigureAwait(false)?

I am trying to understand how async/await keywords works. I have a textblock on a WPF window bind to a TextBlockContent string property and a button which trigger on click...
Jon Skeet
people
quotationmark

It could well be that the task you're running is completing immediately - which means that the await doesn't even need to schedule a continuation. An async method only schedules a continuation when it needs to; if you await something which... more 10/19/2013 8:00:08 PM

people

Why aren't array elements initialized in an enhanced for loop?

When I use normal for-loop, all elements in an array will initialize normally: Object[] objs = new Object[10]; for (int i=0;i<objs.length;i++) objs[i] = new...
Jon Skeet
people
quotationmark

I thought obj refers to a particular element in an array, so if I initialize it, the array element will be initialized as well. Why isn't that happening? No, obj has the value of the array element at the start of the body of the... more 10/19/2013 8:15:28 AM

people

java string with new operator and a literal

Suppose we create a String s as below String s = new String("Java"); so this above declaration will create a new object as the new operator is encountered. Suppose in the same...
Jon Skeet
people
quotationmark

Well, the second line won't create a new object, because you've already used the same string constant in the first line - but s1 and s will still refer to different objects. The reason the second line won't create a new object is that... more 10/19/2013 8:10:11 AM

people

Why bother with abstract or interface classes?

This has been boggling me as to why its better to have an abstract class. So lets say i have to calculate areas of a different shapes (circle, rectangle). I was taught its better...
Jon Skeet
people
quotationmark

It seems like shape class serves no purpose. I can't do an impementation of getArea in shape class, since different shapes calculate area differently. I could just remove shape class and make my code simpler. Suppose you have a... more 10/19/2013 8:04:50 AM

people