Browsing 7239 questions and answers with Jon Skeet

Array Index out of range exception

I want to print out the elements of this multidimensional array but I get the index out of range error. public class test { public static void main(String[] args) { ...
Jon Skeet
people
quotationmark

Yes, because the second "subarray" doesn't have 4 elements. It would be better to do this dynamically: // Note preferred syntax for array types - keep all the type info in one place. int[][] array1 = {{1,2,3,4},{5},{6,7}}; for (int i = 0;... more 11/26/2014 1:22:08 PM

people

Using ObjectWriter. Gives me EOF Exception even though I close the file.

/*This program serializes the students. *If the file exists, then the program loads it. *Otherwise it makes a new class list. *Student objects are added to the class list then...
Jon Skeet
people
quotationmark

This statement: ObjectOutputStream objectOS = new ObjectOutputStream(new FileOutputStream("ClassList.data")); ... overwrites the existing file with an empty file. You then try to read an object from that empty file. You should... more 11/26/2014 12:54:31 PM

people

Add existing key as value to a new key in a java property file

I am using a properties file in my java project for storing the path of various resources. Ex :- Here is my properties file :- MACHINE_NAME = "//pranay" Json_path1 =...
Jon Skeet
people
quotationmark

You can't do this automatically - it's simply not a feature of Java property files. You'll need to write code to do this wherever you plan to load/use the properties file. You should think about: whether you want to make this more... more 11/26/2014 10:04:14 AM

people

MSIL code for accessing a readonly field results in ldarg.0

I have a simple method in C# that accesses a readonly field: IL_0024: ldarg.0 IL_0025: ldfld string MyAssembly.MyClass.TestClass::A My natural assumption was that this...
Jon Skeet
people
quotationmark

Both are correct :) The this value is passed as a first "invisible" parameter to instance methods, so in an instance method, ldarg.0 is "load the this value". You can see the difference if you write a method which uses a different... more 11/26/2014 7:19:48 AM

people

Loop through ICollection and set to null

I need to remove the data from an ICollection if the ID of the collection is in a List of ID's. What I have so far: foreach (var notSelectedToolId in notSelectedToolIds) ...
Jon Skeet
people
quotationmark

You can't, basically - not with just ICollection (or even ICollection<T>). Neither of them allow you to replace an existing element with a new one. With IList<T>, it's pretty easy: var list =... more 11/25/2014 6:07:56 PM

people

Strange java concurrency behavior

I have the following code: class Hi { public static void main (String [] args) { int a = 1; for (int i = 0; i < 50; i++) { a = a + i; ...
Jon Skeet
people
quotationmark

I open two console windows and do java Hi in first one. Then wait for about 10 seconds seconds and do the same in the second window. You're starting two entirely separate process. That's not using multithreading at all - each process... more 11/25/2014 5:59:13 PM

people

Should members of static class in Builder pattern be final?

So I have seen a couple of examples on Builder Pattern. In all, The static class or Builder only has those memebers as final that can be intitialized by the constructor. Example -...
Jon Skeet
people
quotationmark

The design here is that the first and last names always have to be set, but the age is optional. (I would have used Integer here, rather than defaulting to 0... but then I'd prefer to use a birthDate anyway... but I digress.) That... more 11/25/2014 5:34:47 PM

people

Different results for decodeBase64 in Glassfish and Tomcat

I have a value that i encode with special characters, then encode it again using base64, my development environment is the standard uses Netbeans glassfish server, but site is...
Jon Skeet
people
quotationmark

You're using the String(byte[]) constructor without specifying an encoding. Don't do that. Likewise, don't call String.getBytes() without specifying the encoding, either. You're doing that twice in the code you've shown us - and my guess... more 11/25/2014 2:39:44 PM

people

What is the method syntax for this LINQ, is it a join, and if not then what is it?

I find myself using this pattern lots in LINQ: class Thing { public int ID { get; set; } public int ColorID { get; set; } } class Color ...
Jon Skeet
people
quotationmark

It's not performing a join at all, in terms of LINQ understanding. It's just filtering based on two properties where one happens to come from one range variable and the other happens to come from the other. In method syntax you'd write... more 11/25/2014 2:19:30 PM

people

Explicit cast operator vs as with T

So, I came across with the following "problem" and I really curious about the reasons behind it. Consider the following: public class B { } public class A<T> { ...
Jon Skeet
people
quotationmark

The difference is that usually a cast can perform a user-defined conversion if the compiler knows about one. For a generic type, the compiler doesn't have that information. If you want to just perform a straight reference conversion cast,... more 11/25/2014 2:16:58 PM

people