Archive for the 'Collections' Category
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 »
January 14th, 2009 | Posted in Collections, Python | 2 Comments
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 »
December 27th, 2008 | Posted in Collections, Python, Tip | No Comments
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 »
December 17th, 2008 | Posted in Collections, Java, Tip | No Comments
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 »
December 14th, 2008 | Posted in Collections, Java, Tip | No Comments
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 »
December 8th, 2008 | Posted in C#, Collections, Tip | No Comments
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 »
December 8th, 2008 | Posted in C#, Collections, Tip | No Comments
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 »
December 2nd, 2008 | Posted in Collections, Java | 2 Comments
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 »
November 30th, 2008 | Posted in Collections, Python | 1 Comment
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 »
November 30th, 2008 | Posted in Collections, Java | 8 Comments
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 »
November 21st, 2008 | Posted in Collections, Java | 4 Comments
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 »
November 15th, 2008 | Posted in C#, Collections, XmlRpc | No Comments
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 »
November 13th, 2008 | Posted in Collections, Java | 3 Comments
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 »
November 11th, 2008 | Posted in Collections, Java | No Comments
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 »
November 11th, 2008 | Posted in Collections, Java | No Comments
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 »
November 4th, 2008 | Posted in Collections, Java, Performance | 1 Comment
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 »
October 29th, 2008 | Posted in Collections, Java | 8 Comments
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 »
September 27th, 2008 | Posted in Collections, Java, Tip | No Comments