Browsing 7239 questions and answers with Jon Skeet

"There is no deep copy in java," but does it matter for list of Strings?

Let's say I do: public List<E> gimmeAList(List<E> original) { return new ArrayList<E>(original); // this is a shallow memcopy. } I try to modify the new...
Jon Skeet
people
quotationmark

Yes, absolutely - copying a collection of immutable objects is effectively deep, unless you do something crazy like synchronizing on the references or comparing them for reference equality (i.e. operations which depend on object identity). more 12/3/2013 8:48:23 PM

people

Java memory management mystery

I have this piece of code inside a static method of a Java program: import org.w3c.dom.Document; ... Document tempdoc1=tempdoc; //tempdoc1=xmlModifier.setMacro(tempdoc,...
Jon Skeet
people
quotationmark

Well presumably the statement: xmlModifier.setMacro(tempdoc, liquidity, "liquidity"); has some effect on xmlModifier, or tempdoc, or liquidity - so with that commented out, you don't see that effect. My guess is that the method... more 12/3/2013 6:37:30 PM

people

Delegates, Actions and Memory Allocations

I'm currently working on a chunk of code that requires minimum memory allocations. I've noticed if I use a method for a parameter the compiler changes the code and creates a new...
Jon Skeet
people
quotationmark

If you use a lambda expression which doesn't capture anything, the compiler will generate a static field to cache it in. So use that, you can change the Action to an Action<YourClass> and call it with this. So: class YourClass { ... more 12/3/2013 6:29:59 PM

people

Using incremental number as GetHashCode return value

I want to make every instance of a class X having a unique ID. I'm doing this using a incremented number: class X { private int id; private static int instanceCounter =...
Jon Skeet
people
quotationmark

If you want each object to be distinct (so not equal to any other object) then just don't override Equals and GetHashCode at all. The default is to give a "probably unique" hash code, which is all you need to narrow the choices down - and... more 12/3/2013 12:10:40 PM

people

Generating random float between negative and positive opengl es java

Im studying opengles. I want to know how can i generate between -1 to 1. Thats because opengl normalized device coordinates is only between -1 and 1. Some mentioned that random...
Jon Skeet
people
quotationmark

Well Random.nextFloat gives a value greater than or equal to 0, and less than 1 - i.e. a range of [0, 1). So to map that to a range of [-1, 1) you just need to multiply by 2 and subtract 1. float value = random.nextFloat() * 2 - 1; In... more 12/3/2013 7:21:13 AM

people

Indexoutofrangeexception in C#

Can anyone help me to figure out the problem? struct Player { public string Name; public int X; public int Y; } static Player[] players = new...
Jon Skeet
people
quotationmark

This line: static Player[] players = new Player[amountofPlayers]; is executing before you assign a value to amountOfPlayers based on the user input. So amountOfPlayers has its default value of 0, and you're creating an array of 0... more 12/3/2013 6:49:41 AM

people

What exception to throw "Wrong Scenario" (Java)

This is a silly question - but it bugs me. From the existing (library) Java exceptions, which should I throw in the following. I have a method that is used in the wrong scenario...
Jon Skeet
people
quotationmark

I have a method that is used in the wrong scenario (it's basic assumption doesn't hold). That sounds like an exact match for IllegalStateException: Signals that a method has been invoked at an illegal or inappropriate... more 12/2/2013 10:30:09 PM

people

Replacing Special Characters in Strings

I've seen a lot of Q&A here on SO related to this question. And I have used a few examples but something just isn't working: def input = 'now is thé timé' println...
Jon Skeet
people
quotationmark

Given that your second snippet works, I strongly suspect that for the first snippet the encoding you're using in your editor isn't the same as the encoding your Groovy interpreter/compiler is using. In other words, the problem isn't in... more 12/2/2013 8:58:56 PM

people

Dictionary KeyNotFoundException NOT giving error ... WHY?

This question is not why it is giving an error .. rather why it is not giving an error ... private void Form1_Load(object sender, EventArgs e) { Dictionary<string,...
Jon Skeet
people
quotationmark

I suspect you aren't actually running Form1_Load. Here's a short but complete program to demonstrate the exception being thrown as expected: using System; using System.Collections.Generic; using System.Windows.Forms; class Test { ... more 12/2/2013 8:50:58 PM

people

Issue with InternalsVisibleTo attribute

I have two assemblies, say Main and Sub, where Sub depends on Main. Main defines a few classes that have protected internal virtual members, that I want to override in Sub. I...
Jon Skeet
people
quotationmark

Okay, I think I now understand it. While the C# spec doesn't call this out, it doesn't actually mention InternalsVisibleTo at all. I think the way to understand it is that you can't change the acceptable set of call sites for a member by... more 12/2/2013 8:29:55 PM

people