- Home /
Javascript array: Negative Indexes?
I have never tried doing arrays with negative indexes, but, is such possible? Would it work like any normal array?
Answer by Senhor de todo o Mal · Feb 02, 2011 at 04:06 PM
It's possible to create a non zero based Array, using something like this:
var lengths:int[] = new int[1];
var lowerBounds:int[] = new int[1];
lowerBounds[0] = -1;
lengths[0] = 3;
var array:System.Array = System.Array.CreateInstance(Vector3,lengths,lowerBounds);
var vector3Array:Vector3[] = new Array(array).ToBuiltin(Vector3);
Most of the array methods don't support negative indexes though, for example you wouldn't be able to use:
vector3Array[-1] = new Vector3(0.0f,0.0f,0.0f);
But you could use:
vector3Array.GetValue(-1);
vector3Array.SetValue(new Vector3(0.0f,0.0f,0.0f),-1);
Answer by oliver-jones · Nov 25, 2010 at 03:24 AM
I don't see why it wont work. But I'm no expert
Why did this get voted down? The OP just asked if you could do it and he answered simply.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084440im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Array error - Index is less than 0... 3 Answers
Array Index out of range (JavaScript) 1 Answer
IndexOutOfRangeExeption - Array index is out of range 2 Answers
How to check an item's index in an array? 2 Answers
NullReferenceException array 2 Answers