Please note, new blog at http://www.acheron.org/darryl/

ColdFusion vs. Java: Creating an Array of Structures

As I've stated before, I am starting to learn Java. After having read a few chapters on variable types (simple vs. complex), I decided to see if I can create an Array of Structures in Java. I already know that a Structure in ColdFusion is represented as a HashTable in Java, so I thought I'd start there. It didn't take long to learn that a HashMap is now considered the replacement of the HashTable object. I was also after a way of not limiting the size of the Array, given in Java you must specify a size. In ColdFusion of course you can keep adding elements to an array, as they automatically resize. The equivelent of a resizing array/collection in Java is a Vector. Upon exploring this object however, I also found that there was a newer alternative. An ArrayList also allows you to continually add elements, and it resizes automatically. Now, I had identified the object types that I wanted to use, an ArrayList of HashMaps. I could have used a Vector of HashTables, but the only real difference is they are synchronised (and that didn't seem to matter for the purposes of my tests). ColdFusion code <cfcomponent> <cffunction name="arrayOfStructures"> <cfscript> var aData = ArrayNew(1); var stItem = StructNew(); var i = 1; for (i=1; i LTE 10; i=i+1) { stItem = StructNew(); stItem.name = "Item " & i; stItem.description = "Description " & i; ArrayAppend(aData, stItem); } for (i=1; i LTE ArrayLen(aData); i=i+1) { writeoutput(aData[i].name & " : " & aData[i].description & "<br/>"); } </cfscript> </cffunction> </cfcomponent> Java code import java.util.*; public class ArrayListLoop { public static void main (String args[]) { ArrayList<HashMap> al = new ArrayList<HashMap>(); HashMap<String, String> h = new HashMap<String, String>(); Integer i; for (i=1; i <= 10; i++) { h = new HashMap<String, String>(); h.put("name", "Item" + i); h.put("description", "Description" + i); al.add(h); } Iterator it = al.iterator(); while (it.hasNext()) { // Putting (type) before "casts" it HashMap thisMap = (HashMap) it.next(); System.out.println(thisMap.get("name") + " : " + thisMap.get("description")); } // end while loop } // end main() method } // end class

By Anonymous Anonymous, at 4/18/2005 06:22:00 am  

This is an interesting way of learning Java and I like it. First code it in CF and then do the same in Java, I think that ust make it a little easier.



By Blogger Darryl Lyons, at 4/18/2005 07:16:00 pm  

Thanks. It just made sense to me to bring to another language what I have already been doing for years. It has certainly helped me grasp some of the concepts easier.



By Anonymous Anonymous, at 8/01/2006 12:45:00 am  

what about the guaranteed or not guaranteed ordering of elements?



By Blogger Unknown, at 6/21/2010 12:41:00 pm  

The haversack of a lv"
. As for louis vuitton
, the designers acquire their own casting value, even for their Louis vuitton bags
. louis vuitton handbags
is so constant and acquire able adeptness for water-proof and fire-proof.



» Post a Comment