473,480 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Array indexing problem

Consider the following javascript:

var temp = new Array(new Array(0))
document.writeln(temp[0][0])

temp = new Array(new Array(0,1))
document.writeln(temp[0][0])

One would assume that it would print "0 0" that is the first elements
of the arrays, but it prints "undefined 0". Why does temp[0][0] return
undefined when there is only one element in the array but returns the
first element correctly when there are at least two elements?

--
Antti
Jul 20 '05 #1
2 1550
>Consider the following javascript:

var temp = new Array(new Array(0))
document.writeln(temp[0][0])

temp = new Array(new Array(0,1))
document.writeln(temp[0][0])

One would assume that it would print "0 0" that is the first elements
of the arrays, but it prints "undefined 0". Why does temp[0][0] return
undefined when there is only one element in the array but returns the
first element correctly when there are at least two elements?


That is because the behaviour of the Array object is different
when you pass two or more integers to the Array constructor.

If you pass one integer to the Array constructor it will create
an Array of that size. If you pass two or more integers it will
use them as elements in the array.

In your example,

var temp = new Array(new Array(0))

new Array( 0) creates an array of zero length.

temp = new Array(new Array(0,1))

new Array( 0,1) creates an array that contain [ 0, 1].


Peace, Vm
Yaz

Providing complicated solutions to simple problems since 1997.
Jul 20 '05 #2
to heave chunks wrote:
Consider the following javascript:

var temp = new Array(new Array(0))
document.writeln(temp[0][0])

temp = new Array(new Array(0,1))
document.writeln(temp[0][0])

One would assume that it would print "0 0" that is the first elements
of the arrays, but it prints "undefined 0". Why does temp[0][0] return
undefined when there is only one element in the array but returns the
first element correctly when there are at least two elements?


That is because the behaviour of the Array object is different
when you pass two or more integers to the Array constructor.

If you pass one integer to the Array constructor it will create
an Array of that size. If you pass two or more integers it will
use them as elements in the array.


More, how it is interpreted depends on the implementation. To be
sure, use Array literals:

var temp = [[0]];

creates an Array object with an Array object as only element which
only element is zero and stores a reference to it in `temp'. The
"same" can be accomplished with leaving out constructor arguments,
but requiring another variable:

var help = new Array();
help[0] = 0;
var temp = new Array(help);

Note that although temp[0][0] retrieves `0', deleting or overwriting
`help[0]' changes that value since the reference is stored as element,
not the primitive value of zero:

help[0] = 42;
alert(temp.join(",")); // 42
PointedEars

P.S.
Your `From' is borken, read and follow
<http://www.interhack.net/pubs/munging-harmful/>
if you want to be read in the future.
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2729
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
58
10038
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
29
5408
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
10
2050
by: Dennis Myrén | last post by:
Hi. I have an array of struct. My question is, if i do: STRUCT a = new STRUCT ; STRUCT s = new STRUCT(); a = s; since STRUCT is a value type, when assigning element 0 of the STRUCT array...
26
410
by: jacob navia | last post by:
Suppose an implementation where sizeof int == 4 sizeof void * == 8 sizeof long long == 8 When indexing an array array this would mean that arrays are limited to 2GB. To overcome this,
4
1445
by: Grace Fang | last post by:
Hi, I am writing code to sort the columns according to the sum of each column. The dataset is huge (50k rows x 300k cols), so i need to read line by line and do the summation to avoid the...
33
8138
by: Zytan | last post by:
I want to make a zero element array. I know that Nothing is not the same as a zero element array, since I can't get the length of, or iterate through, an array = Nothing. I could make a zero...
4
26319
by: Chiefy | last post by:
C# APP Basically i am trying to use indexing on an array that i have fed into a method as its parameter. Here is a simplified version of my code. public void Inputs(string path) { ...
3
6813
by: Rüdiger Werner | last post by:
Hello! Out of curiosity and to learn a little bit about the numpy package i've tryed to implement a vectorised version of the 'Sieve of Zakiya'. While the code itself works fine it is...
5
2659
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7084
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6929
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4779
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
181
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.