How to Store Integers, Doubles, etc. in ArrayList in Java

You may be reading this post because you are trying to do something like the following code, which isn’t allowed in Java because int is a “primitive type”:

ArrayList<int> intList = new ArrayList<int>();
intList.add(1);
intList.add(2);

The short answer is to use syntax such as the following:

ArrayList<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
 
for (int i : intList)
System.out.println(i);

Now for [...] Read more »

Parse String to Date in Java

Another post explains how to create a Date instance in Java. But if you have a string and want to convert it to a Date, you go about it a completely different way (yes, you may be able to hear a hint of sarcasm in my voice). Below is a “simple” solution. You [...] Read more »

Creating Instances of Java Date Class

I have a hard time understanding why Java made it so difficult to create instances of dates, but I’m sure there was some logical reason. You would think you could use a constructor or factory method on the Date class, but this doesn’t exist or has been deprecated. So what I have been able to [...] Read more »

Find Files in Directory Using Java

It’s surprisingly (or not, depending on your opinion of Java) difficult to get a list of files matching a pattern in a directory. For example, C# makes this really easy. In Java, you need to create a class that implements the java.io.FileFilter interface.
Below is a simple example of how this interface could implemented:

import [...] Read more »

Invoke a Command-line Program Using Python

In Windows, Linux, MacOS, etc. you can either start up programs using the user interface (pointing and clicking with your mouse) or from a command line. When you run something from the command line, it does basically the same thing behind the scenes, but you type commands instead.
For example, in Windows, you can open Internet [...] Read more »

Convert a Delimited String to a List in Python

Let’s say you have a string with values that are delimited by a comma. This would be common when parsing text files.
So suppose you have a delimited string:

x = "abc,def,ghi"

You can convert that into a Python list using the split function, passing the character that is delimiting the items:

y = x.split(",")
print y // [’abc’, ‘def’, [...] Read more »

Rank Numbers in Java

If you have a list of values in Java, you sometimes want to sort them. Other times, rather than sort them, you may want to determine the order that they would be sorted…or in other words rank them.
So if you create a list in this way:

ArrayList list = new ArrayList();
list.add(0);
list.add(3);
list.add(1);

…and sorted it, the list would [...] Read more »