• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Popluateing a 2d array

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. Sorry if this is a silly question, but it's been driveing me mad.
The following code should have an output of:

(0.0) (0.1) (0.2)
(1.0) (1.1) (1.2)
(2.0) (2.1) (2.2)

but the actualy output is:

(2.0) (2.1) (2.2)
(2.0) (2.1) (2.2)
(2.0) (2.1) (2.2)

It seems my double for loops makes the current row of data overwrite all the preceeding rows with it's own data. I'm guessing that I've implented my loops incorrectly. But I cant see how. Can anyone help? Thanks in advance


[ August 29, 2006: Message edited by: Bear Bibeault ]
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaScript arrays do not work like
UserBoard[i,j]

they work like

UserBoard[i][j]

Eric
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should use something like this code, it will work fine

<script language="JavaScript">
var UserBoard = new Array(20);
for(var i=0;i<=21;i++)
{
UserBoard[i]=new Array(20);
}
for(var i=0;i<3;i++)
{for(var j=0;j<3;j++)
{UserBoard[i][j]=i+'.'+j;
}
}
alert("test:" + UserBoard[1][1]);
for(var k=0;k<3;k++){for(var l=0;l<3;l++)
{
document.write('(' +UserBoard[k][l] + ') ');
}
document.write('<br>');}
</script>

Please nite that you are having one equlaity instead of assignment in line two.. and store two dimensional arrays like arr[i][j] instead of arr[i,j].
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

var UserBoard = new Array(20);

for(var i=0;i<=21;i++)
{
UserBoard[i]==new Array(20);
}


This looks suspect. You're allocating a 20 element one-dimensional array, then you proceed to fill it with 22 elements, each of which is another 20 element array. Except that you're not actually assigning, but comparing ("==" instead of "="). Further down you access the array as if it was two-dimensional.
 
Chaz Andrews
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys, that worked That's how i would have done it if I hadn't read this page (which is the second result in google for 'javascript 2d array) http://www.iceteks.com/forums/archive/t/3987/

May I ask, is that page wrong then? And also, do you know why my array was being populated in such a weird way useing that method?
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic