Browsing 7239 questions and answers with Jon Skeet

Is `log(n)` base 10?

Still getting a grip on logarithms being the opposite of exponentials. (Would it also be correct to describe them as the inversion of exponentials?) There are lots of great SO...
Jon Skeet
people
quotationmark

You don't need to worry about the base - if you're dealing with algorithmic complexity, it doesn't matter which base you're in, because the difference is just a constant factor. Fundamentally, you just need to know that log n means that... more 8/29/2014 4:44:35 PM

people

How do I return an array after it has been swapped?

So I have my main method which is supposed to have an array of 1-20 print, and then I am supposed to pass that array in a loop for every even number occurrence. The swap method...
Jon Skeet
people
quotationmark

You don't need to return anything (assuming you change the method signature to return void) - the elements of the array have been swapped in-place. For example: int[] array = { 3, 5, 4 }; swap(array, 0, 2); System.out.println(array[0]);... more 8/28/2014 8:09:27 PM

people

How to set a double method equal to a value in a static method?

I have a method called getX1() which gets data from a different class: public double getX1(){ double x1 = getIntent().getExtras().getDouble("s_xd2"); return...
Jon Skeet
people
quotationmark

It says: Non-static method 'getX1()' cannot be referenced from a static context Right - that's got nothing to do with creating an array, or anything like that. The problem is that getX1() is an instance method - it needs to operate... more 8/28/2014 6:00:58 PM

people

Trying to display a random card in a weird way with java

Right now I am trying to display a random card after defining a full deck. I was wondering if something along the lines of: g.drawImage( card (int) * (Math.random() * 52),...
Jon Skeet
people
quotationmark

No, that's not the way Java works. You can't use variable names determined at execution time. Instead of having 52 separate variables, you should have an array or a collection: private final Random random = new Random(); private final... more 8/28/2014 5:57:25 PM

people

'void' type not allowed here listener to a parameter

Why you can't add a listener to a object which is a parameter? panelThird.add(new JTextField( "Write here !" ).addActionListener( new ActionListener(){ public...
Jon Skeet
people
quotationmark

addActionListener has a return type of void - so you can't write: panelThird.add(new JTextField(...).addActionListener(...)); Instead, you need: JTextField field = new... more 8/28/2014 4:55:48 PM

people

What will be the return variable for 'int' type function (sql query function)

Below I declare a function in witch type is 'int'. I am declare this function for sql query that give int as output. Here I have to return something . But I can't understand what...
Jon Skeet
people
quotationmark

Well, you've got a query... but for some reason you're calling ExecuteNonQuery. You should be specifying ExecuteScalar in order to fetch a single value. You can then cast the result to int: return (int) command.ExecuteScalar(); I assume... more 8/28/2014 2:39:20 PM

people

Put inside the extended class the parent class

I have a problem with an extended class. This are the classes: class A{ } class B extends A{ } Now the problem is that I have a method that returns A , something like...
Jon Skeet
people
quotationmark

Fundamentally I think you've missed what the assignment operator does. This statement: b = doSomething(); changes the value of b to whatever the doSomething() method returns. It doesn't depend on the existing value of b at all. So... more 8/28/2014 1:46:23 PM

people

Java Core: data type for variable of changing value

it's a bit of a nooby question, but say I've got a variable that keeps getting re-initialized within a loop, what would be the best data-type to store that data temporarily? for...
Jon Skeet
people
quotationmark

The reason I ask is that I know that Strings are immutable, which suggests that there is a more efficient alternative, am I right? Nope. You're calling readLine multiple times, that's going to give you a different string reference... more 8/28/2014 12:09:03 PM

people

Interpret java.util.Date

I have a system where I through a third party lib gets a date. I know for a fact that the date entered is 1914-08-28 and I don't care for the hours and minutes. I need to deliver...
Jon Skeet
people
quotationmark

A java.util.Date isn't in any particular time zone - it's just an instant in time, which will have different local times around the world. Internally, it's a number of milliseconds since the Unix epoch, but frankly it could be a different... more 8/28/2014 8:56:07 AM

people

how to prevent method from being instantiated

How I can Prevent interface method that I implemented in a class from being instantiated. public interface Interface1 { void Show(); } public class ABC : Interface1 { ...
Jon Skeet
people
quotationmark

You can use explicit interface implementation, if you really only want the method to be available via an expression of the interface type: void Interface1.Show() { throw new NotImplementedException(); } You can't remove the... more 8/28/2014 6:40:06 AM

people