Code Comments

Code Comments

Tips and short tutorials on various programming technologies

Code Comments RSS Feed
 
 
 
 

Archive for Collections

Creating a Reverse Sequence of Numbers in Python

Let’s say you want to create a sequence of numbers in reverse order (for example, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1). I tried to do this with the range function, but I couldn’t find a way to do it. But Python has a built-in function called reversed that allows you to [...] Read more »

Cast a List from One Type to Another in Python

One of the key properties of Python is that it is a weakly typed language. In contract to strongly typed languages such as Java, you don’t have to define the class of an object when you instantiate it. This offers much flexibility. But sometimes you want to be able to define precisely what class of [...] Read more »

Find the Difference Between Two Arrays in Java

Please refer to this post for explanation of how to do this using Lists in Java. If you wanted to do the same thing with arrays, you would simply need to convert the arrays to ArrayLists before you find the difference. Read more »

Force Collections to Accept Only One Data Type in Java

Collections, such as ArrayList and Set, in Java used to be able to contain any class. So you could implement the following code without a problem.

ArrayList oldWay = new ArrayList();
oldWay.Add("xyz");
oldWay.Add(456);
oldWay.Add(new CustomWhatever());

But sometimes this flexibility caused problems because you might want to limit what could be contained in that list (for example, limit it to String [...] Read more »

Force Collections to Accept Only One Class in C#

Collections, such as List and ArrayList, in C# used to be able to contain any class. So you could implement the following code without a problem.

ArrayList oldWay = new ArrayList();
oldWay.Add("abc");
oldWay.Add(123);
oldWay.Add(new CustomWhatever());

But sometimes this flexibility caused problems because you might want to limit what could be contained in that list (for example, limit it to string [...] Read more »

Example of Implementing IEnumerable and IEnumerator in C#

In the .NET Framework, there are two interfaces designed to allow you to iterate easily over collections of objects as you would typically do in a for loop. Many classes in the .NET Framework have implemented these interfaces and do their work behind the scenes so you don’t have to worry about how it is [...] Read more »

Find an Object in an ArrayList with Java

Let’s say you have an ArrayList in Java.

ArrayList<String> a = new ArrayList<String>();
a.add("abc");
a.add("def");
a.add("ghi");

Now let’s say you wanted to grab the “def” object out of it. You could do this in the following way:

String b = a.get(a.indexOf("def"));

You can only retrieve an object from an ArrayList by its index. So a simple workaround is first to find the [...] Read more »

Find the Difference Between Two Lists with Python

Let’s say you have two lists in Python that have a lot of overlap between them.

a = ["abc","def","ghi"]
b = ["def","ghi","jkl"]

Now let’s say you want to determine what is in b that is not in a. In set theory, you would refer to this as the set-theoretic difference of b in a. The following simple code [...] Read more »

Find the Difference Between Two Lists in Java

Let’s say you have two lists in Java that have a lot of overlap between them.

ArrayList a = new ArrayList();
a.add("abc");
a.add("def");
a.add("ghi");
 
ArrayList b = new ArrayList();
b.add("def");
b.add("ghi");
b.add("jkl");

Now let’s say you want to determine what is in b that is not in a. In set theory, you would refer to this as the set-theoretic difference of b in a. [...] Read more »

Add an Item to an Array in Java

If you haven’t read this post that talks about the differences between arrays and ArrayLists, that would be a good first step. Typically you would use an ArrayList rather than an array if you want to be able to add and remove objects. However, if you would rather (or are required to) use arrays, this [...] Read more »

Publishing to a WordPress Blog via XML-RPC (Part 3)

Please refer to this post for some background.
OK, this post is going to talk about some of the plumbing. When you are sending requests to the server and when the server is replying with responses, you need to deal with key-value pairs. For example, userName = “steve” or password = “abc123″. C# has some classes [...] Read more »

Convert an ArrayList to an Array in Java

OK, back on my soap box; this task is somewhat annoying in Java and should be easier.
If you have an ArrayList object and want to convert it to an array of objects, here’s the way to do it with a minimal amount of code.

ArrayList list = new ArrayList();
list.add("abc");
list.add("def");
list.add("ghi");
 
Object[] array = list.toArray();
 
for (Object x : [...] Read more »

Convert an Array to an ArrayList in Java

Sometimes you have an array but would rather use an ArrayList. This post talks about the differences between these. It’s not super straightforward to do this conversion but not too hard either. Here’s how you would do it for strings.

public static ArrayList<String> CreateStringList(String … values)
{
ArrayList<String> results = new ArrayList<String>();
[...] Read more »

Convert Java List to Delimited String

In this post, I explained how to take a Python list and convert it to a delimited string. It’s a little trickier to do this in Java but not too bad. Below is some code for doing this with either an ArrayList or an array.
Here are the methods:

import java.util.*;
 

 
public static String Join(String[] s, String delimiter)
{
[...] Read more »

What Is the Difference Between Arrays and ArrayLists in Java

This will not be an exhaustive answer. To understand the full details, you’ll need to look at the Java documentation provided by Sun Microsystems.
An array in Java is immutable, which means that when you create it, it is stored in a specific place in memory and never moved until it is no longer needed and [...] Read more »

Fast Way to Get All Unique Values from a List in Java

Let’s say you have an ArrayList of string values and want to get all unique values from it. A seemingly obvious way to do this would be to create a list and before adding the next item to check that an identical item doesn’t already exist in the list.

ArrayList uniqueVals = new ArrayList();
 
for (Object x [...] Read more »

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 »