All Questions
24,196,085
questions
4321
votes
35
answers
1.8m
views
What are the differences between a HashMap and a Hashtable in Java?
What are the differences between a HashMap and a Hashtable in Java?
Which is more efficient for non-threaded applications?
4283
votes
40
answers
5.4m
views
How do I UPDATE from a SELECT in SQL Server?
In SQL Server, it is possible to insert rows into a table with an INSERT.. SELECT statement:
INSERT INTO Table (col1, col2, col3)
SELECT col1, col2, col3
FROM other_table
WHERE sql = 'cool'
Is it ...
4274
votes
34
answers
3.5m
views
How to insert an item into an array at a specific index?
I am looking for a JavaScript array insert method, in the style of:
arr.insert(index, item)
Preferably in jQuery, but any JavaScript implementation will do at this point.
4229
votes
1
answer
3.2m
views
The Definitive C++ Book Guide and List
This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year.
Unlike many other programming languages, which are often picked up on the go from ...
4227
votes
33
answers
3.6m
views
Is there a CSS parent selector?
How do I select the <li> element that is a direct parent of the anchor element?
As an example, my CSS would be something like this:
li < a.active {
property: value;
}
Obviously there are ...
4218
votes
94
answers
4.6m
views
How to round to at most 2 decimal places, if necessary
I'd like to round at most two decimal places, but only if necessary.
Input:
10
1.7777777
9.1
Output:
10
1.78
9.1
How can I do this in JavaScript?
4199
votes
38
answers
440k
views
How can I pair socks from a pile efficiently?
Yesterday I was pairing the socks from the clean laundry and figured out the way I was doing it is not very efficient. I was doing a naive search — picking one sock and "iterating" the pile in ...
4196
votes
78
answers
2.6m
views
How can I validate an email address using a regular expression?
Over the years I have slowly developed a regular expression that validates most email addresses correctly, assuming they don't use an IP address as the server part.
I use it in several PHP programs, ...
4191
votes
35
answers
1.7m
views
What exactly is RESTful programming?
What exactly is RESTful programming?
4169
votes
36
answers
946k
views
What is the !! (not not) operator in JavaScript?
I saw this code:
this.vertical = vertical !== undefined ? !!vertical : this.vertical;
It seems to be using !! as an operator, which I don't recognize. What does it do?
4157
votes
61
answers
3.4m
views
Sort array of objects by string property value
I have an array of JavaScript objects:
var objs = [
{ first_nom: 'Laszlo', last_nom: 'Jamf' },
{ first_nom: 'Pig', last_nom: 'Bodine' },
{ first_nom: 'Pirate', last_nom: '...
4148
votes
142
answers
4.7m
views
Message 'src refspec master does not match any' when pushing commits in Git
I clone my repository with:
git clone ssh://xxxxx/xx.git
But after I change some files and add and commit them, I want to push them to the server:
git add xxx.php
git commit -m "TEST"
git push ...
4132
votes
13
answers
1.5m
views
grep: show lines surrounding each match [closed]
How do I grep and show the preceding and following 5 lines surrounding each matched line?
4130
votes
54
answers
3.4m
views
How do I get the current branch name in Git?
How do I get the name of the current branch in Git?
4119
votes
34
answers
7.5m
views
How can I iterate over rows in a Pandas DataFrame?
I have a pandas dataframe, df:
c1 c2
0 10 100
1 11 110
2 12 120
How do I iterate over the rows of this dataframe? For every row, I want to access its elements (values in cells) by the name ...
4107
votes
42
answers
1.9m
views
Create ArrayList from array
Element[] array = {new Element(1), new Element(2), new Element(3)};
How do I convert the above variable of type Element[] into a variable of type ArrayList<Element>?
ArrayList<Element> ...
4107
votes
59
answers
5.2m
views
How do I generate random integers within a specific range in Java?
How do I generate a random int value in a specific range?
The following methods have bugs related to integer overflow:
randomNum = minimum + (int)(Math.random() * maximum);
// Bug: `randomNum` can be ...
4080
votes
33
answers
3.7m
views
Checking if a key exists in a JavaScript object?
How do I check if a particular key exists in a JavaScript object or array?
If a key doesn't exist, and I try to access it, will it return false? Or throw an error?
4077
votes
7
answers
4.9m
views
How do I clone a specific Git branch? [duplicate]
Git clone will clone remote branch into local.
Is there any way to clone a specific branch by myself without switching branches on the remote repository?
4058
votes
23
answers
1.8m
views
Make an existing Git branch track a remote branch?
I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch?
I know I can just edit the .git/config file, but it seems there should be an ...
4054
votes
55
answers
5.6m
views
How do I check for an empty/undefined/null string in JavaScript?
Is there a string.Empty in JavaScript, or is it just a case of checking for ""?
4039
votes
46
answers
5.4m
views
Loop through an array in JavaScript
In Java, you can use a for loop to traverse objects in an array as follows:
String[] myStringArray = {"Hello", "World"};
for (String s : myStringArray) {
// Do something
}
Can ...
4030
votes
12
answers
369k
views
Proper use cases for Android UserManager.isUserAGoat()?
I was looking at the new APIs introduced in Android 4.2.
While looking at the UserManager class I came across the following method:
public boolean isUserAGoat()
Used to determine whether the ...
4027
votes
21
answers
3.1m
views
How do I tell if a file does not exist in Bash?
This checks if a file exists:
#!/bin/bash
FILE=$1
if [ -f $FILE ]; then
echo "File $FILE exists."
else
echo "File $FILE does not exist."
fi
How do I only check if the ...
3989
votes
46
answers
3.4m
views
How do I efficiently iterate over each entry in a Java Map?
If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map?
Will the ordering of ...
3982
votes
27
answers
4.1m
views
How to use a global variable in a function?
How do I create or use a global variable inside a function?
How do I use a global variable that was defined in one function inside other functions?
Failing to use the global keyword where appropriate ...
3967
votes
36
answers
587k
views
Is floating-point math broken?
Consider the following code:
0.1 + 0.2 == 0.3 -> false
0.1 + 0.2 -> 0.30000000000000004
Why do these inaccuracies happen?
3950
votes
14
answers
1.1m
views
Move existing, uncommitted work to a new branch in Git
I started some work on a new feature and after coding for a bit, I decided this feature should be on its own branch.
How do I move the existing uncommitted changes to a new branch and reset my ...
3924
votes
15
answers
1.2m
views
Remove a file from a Git repository without deleting it from the local filesystem
I want to remove a file from my repository.
git rm file_to_remove.txt
will remove the file from the repository, but it will also remove the file from the local file system. How do I remove this file ...
3920
votes
44
answers
1.3m
views
What are the differences between a pointer variable and a reference variable?
What is the difference between a pointer variable and a reference variable?
3910
votes
32
answers
2.1m
views
How do I cast int to enum in C#?
How do I cast an int to an enum in C#?
3902
votes
27
answers
2.5m
views
View the change history of a file using Git versioning
How do I view the history of an individual file with complete details of what has changed?
git log -- [filename] shows me the commit history of a file, but how do I see the file content that changed?
3897
votes
54
answers
4.4m
views
How do I get the current time in Python?
How do I get the current time in Python?
3890
votes
41
answers
4.3m
views
How do I test for an empty JavaScript object?
After an AJAX request, sometimes my application may return an empty object, like:
var a = {};
How can I check whether that's the case?
3858
votes
6
answers
1.5m
views
How to catch multiple exceptions in one line? (in the "except" block)
I know that I can do:
try:
# do something that may fail
except:
# do this if ANYTHING goes wrong
I can also do this:
try:
# do something that may fail
except IDontLikeYouException:
# ...
3856
votes
11
answers
318k
views
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
Until today, I thought that for example:
i += j;
Was just a shortcut for:
i = i + j;
But if we try this:
int i = 5;
long j = 8;
Then i = i + j; will not compile but i += j; will compile fine.
...
3850
votes
17
answers
519k
views
Why is char[] preferred over String for passwords?
In Swing, the password field has a getPassword() (returns char[]) method instead of the usual getText() (returns String) method. Similarly, I have come across a suggestion not to use String to handle ...
3832
votes
21
answers
3.8m
views
How to copy files
How do I copy a file in Python?
3830
votes
31
answers
4.3m
views
How do I POST JSON data with cURL?
I use Ubuntu and installed cURL on it. I want to test my Spring REST application with cURL. I wrote my POST code at the Java side. However, I want to test it with cURL. I am trying to post a JSON data....
3825
votes
96
answers
5.0m
views
What is the JavaScript version of sleep()?
Is there a better way to engineer a sleep in JavaScript than the following pausecomp function (taken from here)?
function pausecomp(millis)
{
var date = new Date();
var curDate = null;
do ...
3792
votes
14
answers
2.5m
views
What is __init__.py for?
What is __init__.py for in a Python source directory?
3785
votes
23
answers
5.1m
views
Convert bytes to a string in Python 3
I captured the standard output of an external program into a bytes object:
>>> from subprocess import *
>>> stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
>>> ...
3783
votes
33
answers
1.3m
views
How do I stash only one file out of multiple files that have changed?
How do I stash only one of the multiple changed files on my branch?
3778
votes
26
answers
3.8m
views
Get the current URL with JavaScript?
All I want is to get the website URL. Not the URL as taken from a link. On the page loading I need to be able to grab the full, current URL of the website and set it as a variable to do with as I ...
3770
votes
7
answers
4.5m
views
Iterate through a HashMap [duplicate]
What's the best way to iterate over the items in a HashMap?
3764
votes
82
answers
2.4m
views
How do I correctly clone a JavaScript object?
I have an object x. I'd like to copy it as object y, such that changes to y do not modify x. I realized that copying objects derived from built-in JavaScript objects will result in extra, unwanted ...
3735
votes
61
answers
754k
views
How can I create a memory leak in Java?
I just had an interview where I was asked to create a memory leak with Java.
Needless to say, I felt pretty dumb, having no idea how to start creating one.
What would an example be?
3724
votes
28
answers
1.0m
views
What is the difference between __str__ and __repr__?
What is the difference between __str__ and __repr__ in Python?
3720
votes
26
answers
3.0m
views
How do I create a remote Git branch?
I created a local branch. How do I push it to the remote server?
UPDATE: I have written a simpler answer for Git 2.0 here.
3711
votes
32
answers
3.1m
views
Git refusing to merge unrelated histories on rebase
During git rebase origin/development the following error message is shown from Git:
fatal: refusing to merge unrelated histories
Error redoing merge 1234deadbeef1234deadbeef
My Git version is 2.9.0. ...