Hi Gert, thanks for replying here.
I dunno that I agree with your assertion there: there
is an element at index position 2, it's just null.
This can be borne out by looking at the underlying Vector, and having a poke around:
<cfscript>
a = arrayNew(1);
a[1] = "tahi";
a[3] = "rua";
a[5] = "rima";
a[7] = "whitu";
arrayDeleteAt(a, 7); // 6th / last element is empty
v = createObject("java", "java.util.Vector").init(a);
</cfscript>
<cfoutput>
arrayLen(a): #arrayLen(a)#<br /> <!--- this outputs "6" because there IS an element at index point 6 --->
v.size(): #v.size()#<br /> <!--- this outputs "6" because there IS an element at index point 6 --->
<cfset e = v.elementAt(1)> <!---ie: the SECOND element. This doesn't error because there IS a second element--->
<!---and, yeah, I get that if I go a[2] I get an error. I think THIS is the incorrect behaviour in all this --->
isDefined(): #isDefined("variables.e")#<br /><!---"not defined"... as we know this is how CF deals with NULL --->
structKeyExists(): #structKeyExists(variables, "e")#<br /><!--- ditto --->
<cftry>
<cfset e = v.elementAt(6)><!--- (bear in mind that's the SEVENTH element... this is a Java method, so the indexes are zero-based) ie: def doesn't exist in the Vector, so demonstrates what happens when we access the element at an index that doesn't exist. Because the behaviour here is different from when we accessed the second element, it further proves that there IS a second element --->
<!---this has errored now, so the next coupla lines don't run --->
isDefined(): #isDefined("variables.e")#<br />
structKeyExists(): #structKeyExists(variables, "e")#<br />
<cfcatch>
<cfdump var="#cfcatch#"><!--- "java.lang.ArrayIndexOutOfBoundsException: 6 >= 6 at java.util.Vector.elementAt(Vector.java:427" --->
</cfcatch>
</cftry>
toString(): #a.toString()#<br /> <!---shows the nulls --->
serializeJson(a): #serializeJson(a)#<br /> <!---shows the nulls --->
<cfdump var="#variables#"><!--- shows the nulls (somewhat inelegantly) --->
</cfoutput>
So if I want to swap index position 2 (null) & 3 (rua), then that's exactly what I do: put a null in a[3], and put a "rua" into a[2] (obviously one of those is going to go into a temporary variable during the swap, but you get / already knew the idea).
Thoughts?
--
Adam