473,699 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

make array empty

Hi, Can someone tell me, How to redefine array or make array empty or
null.

Here what I am trying to do.
var temp = new Array();
for(i=0; i <=outstring.len gth-1; i++) {
temp = outstring[i].split(',');
}

Once the for loop will finish doing it, I want temp array to be null
or blank.
How Can I do that?
I tried doing temp.length =0 but that didn't work.
Oct 8 '08 #1
8 5844
On Oct 8, 3:10*pm, Sunny <sunnyluth...@g mail.comwrote:
Hi, Can someone tell me, How to redefine array or make array empty or
null.

Here what I am trying to do.
var temp = new Array();
for(i=0; i <=outstring.len gth-1; i++) {
temp = outstring[i].split(',');

}

Once the for loop will finish doing it, I want temp array to be null
or blank.
How Can I do that?
I tried doing temp.length =0 but that didn't work.
temp=[];
Oct 8 '08 #2
On 2008-10-08 16:10, Sunny wrote:
Hi, Can someone tell me, How to redefine array or make array empty or
null.

Here what I am trying to do.
var temp = new Array();
for(i=0; i <=outstring.len gth-1; i++) {
temp = outstring[i].split(',');
}
I assume you're doing something else in the loop too, or this would be
pretty pointless.

First of all, you don't need

var temp = new Array();

because you'll immediately assign something else to temp in the loop.

var temp;

will do. By the way, if you do want to create an empty array, use an
array literal:

var temp = []; // unless see below[*]
Once the for loop will finish doing it, I want temp array to be null
or blank.
Why? Just let it go out of scope, and it will be garbage collected
(unless there are closures present).
How Can I do that?
I tried doing temp.length =0 but that didn't work.
Are you really sure about that?
How didn't it work?

Alternatively, you could also use the delete operator, or you could
assign something else to temp, if you want, like null or [].
- Conrad

[*] That is, unless you're going push() a lot of elements on the array,
and you know the final number in advance; then it's more efficient to
use "new Array(num_eleme nts)". I'm not exactly sure why that is, because
the implementations don't reserve memory in advance; maybe it's that the
..length property doesn't change after each push().
Oct 8 '08 #3
On Oct 9, 12:42*am, Conrad Lender <crlen...@yahoo .comwrote:
[...]
>
* var temp = []; * // unless see below[*]
[...]
>[*] That is, unless you're going push() a lot of elements on the array,
and you know the final number in advance; then it's more efficient to
use "new Array(num_eleme nts)". I'm not exactly sure why that is, because
the implementations don't reserve memory in advance; maybe it's that the
.length property doesn't change after each push().
Have you compared that to using a while loop? e.g.

var t = [];
var i = array.length;

while (i--) {
t.push(array[i]);
}

That should only set length once too, but I imagine the internal
[[put]] method must still check whether length needs to be
incremented, even if it doesn't have to actually do it.
--
Rob
Oct 9 '08 #4
On Oct 8, 7:42*pm, Conrad Lender <crlen...@yahoo .comwrote:
On 2008-10-08 16:10, Sunny wrote:
Hi, Can someone tell me, How to redefine array or make array empty or
null.
Here what I am trying to do.
var temp = new Array();
for(i=0; i <=outstring.len gth-1; i++) {
temp = outstring[i].split(',');
}

I assume you're doing something else in the loop too, or this would be
pretty pointless.

First of all, you don't need

* var temp = new Array();

because you'll immediately assign something else to temp in the loop.

* var temp;

will do. By the way, if you do want to create an empty array, use an
array literal:

* var temp = []; * // unless see below[*]
Once the for loop will finish doing it, I want temp array to be null
or blank.

Why? Just let it go out of scope, and it will be garbage collected
(unless there are closures present).
How Can I do that?
I tried doing temp.length =0 but that didn't work.

Are you really sure about that?
How didn't it work?

Alternatively, you could also use the delete operator, or you could
assign something else to temp, if you want, like null or [].

* - Conrad
[*] That is, unless you're going push() a lot of elements on the array,
and you know the final number in advance; then it's more efficient to
use "new Array(num_eleme nts)". I'm not exactly sure why that is, because
the implementations don't reserve memory in advance; maybe it's that the
.length property doesn't change after each push().
IMO, memory is reserved for the data structure which is responsible
for maintaining the state of the Array object; it can be an Object[]
of Java or an array of void pointers in C. The advantage AFAIK here is
that setting the 'length' property avoids a lot of copying of
references to and fro when inserting an element since each time the
backing data store has to be expanded to take in the new element i.e.
ensureCapacity does nothing as long as the number of elements
sequentially inserted is less than the specified length.

Even I think that setting the length property to 0 should have done
the job though setting it to null or just letting it go out of scope
of more convenient and processing friendly [since setting the length
or deleting actually loops over the array elements].
Oct 9 '08 #5
On Wed, 8 Oct 2008 at 22:53:26, in comp.lang.javas cript, sasuke wrote:

<snip>
>IMO, memory is reserved for the data structure which is responsible
for maintaining the state of the Array object; it can be an Object[]
of Java or an array of void pointers in C. The advantage AFAIK here is
that setting the 'length' property avoids a lot of copying of
references to and fro when inserting an element since each time the
backing data store has to be expanded to take in the new element i.e.
ensureCapaci ty does nothing as long as the number of elements
sequentially inserted is less than the specified length.
It can't be as simple as that. Try putting this into the address bar and
executing it :

javascript: var a1 = new Array(); a1[2000000000] = "Wow"; alert(a1.length );

It doesn't blow up. Neither does this :

javascript: var a1 = new Array(200000000 0); alert(a1.length );

>Even I think that setting the length property to 0 should have done
the job though setting it to null or just letting it go out of scope
of more convenient and processing friendly [since setting the length
or deleting actually loops over the array elements].
According to ECMA 262, setting the length to zero should indeed do the job :

"whenever the length property is changed, every property whose
name is an array index whose value is not smaller than the new length is
automatically deleted."

As you say, replacing the variable's value by a new array or null will also do
the job. Whether it is quicker depends on how the garbage collector works.

John
--
John Harris
Oct 9 '08 #6
John G Harris wrote:
On Wed, 8 Oct 2008 at 22:53:26, in comp.lang.javas cript, sasuke wrote:

ECMAScript does not have real arrays. Arrays are objects.
>
It can't be as simple as that. Try putting this into the address bar and
executing it :

javascript: var a1 = new Array(); a1[2000000000] = "Wow"; alert(a1.length );
That creates a new Array with two properties: 2000000000 and length.

2000000000 = "Wow"
length = 2000000001
It doesn't blow up. Neither does this :

javascript: var a1 = new Array(200000000 0); alert(a1.length );

That creates a new Array with one property: length.

Firefox <= 3.0.3 exhibits a bug where an array is 'prefilled' with
properties having undefined values.

javascript:aler t('2' in [,,,,,,,,,,])

true in firefox 3.0.3

(should be false).
Garrett
>
John
Oct 10 '08 #7
sasuke wrote:
[...] Though there is no mention of such in the specification, almost all
implementations have a `sparse' or `dense' flag which is used to mark the
Array object. This flag demands a suitable processing on part of the
implementation when a high value of index is used; which explains your
first e.g. [...]
How do you got *that* idea?
As for the second e.g., try something like javascript: var a1 = new
Array(200000000 0); for(var i = 0, maxI = a1.length; i < maxI; ++i) {
a1[i] = Number(i); }

The script stops responding, doesn't it?
Responding to what?
This gives us an indication that the implementation is smart enough to
allocate memory to the Array object when it actually is required [maybe
the length passed to the Array constructor isn't used till a reference to
a particular location isn't made].
Your logic is flawed. The *user agent* stops responding (and may show the
user a dialog that allows them to stop execution of the script) because the
script is running quite a long time and all known ECMAScript implementations
are single-threaded. It is completely irrelevant that an Array object is
involved here. Simple proof:

while (true);
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Oct 10 '08 #8
sasuke wrote:
Thomas 'PointedEars' Lahn wrote:
>sasuke wrote:
>>[...] Though there is no mention of such in the specification, almost all
implementatio ns have a `sparse' or `dense' flag which is used to mark the
Array object. This flag demands a suitable processing on part of the
implementatio n when a high value of index is used; which explains your
first e.g. [...]
How do you got *that* idea?

When sifting through the source code of Rhino; the Java implementation
of ECMAScript. The NativeArray class maintains a flag called
'denseOnly'.
While that is interesting, this is but one implementation, the wrong one for
the discussed example (we are talking SpiderMonkey and friends in this
thread), and it does not explain the example (whereas it is unclear what
exactly needed explaining there).
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Oct 11 '08 #9

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

Similar topics

5
2211
by: LRW | last post by:
I did a Web search, and a deja.com search on this...and I'm finding how to make checkboxes act like radiobuttons, and other interesting behaviors, but nothing that quite answers my question. If someone has a link or thread they know about, even just that would be great! What I have is a page that generates a list of items from a database...a list of rows. I'm having each row in the WHILE array create a checkbox, so that someone can put...
5
6523
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with the object references. However, I just realized that indices may only be referenced by strings.
8
2384
by: Gustaf Liljegren | last post by:
I'd like a simple way of extracting everything that's not whitespace on each line in a file. For example, if the program encounters this line 1 2\t\t3 \t\t4 I want a string array like this {"1", "2", "3", "4"} I tried working with String.Split() but found that it doesn't automatically
1
1635
by: ad | last post by:
I select a single field from a table of database, like: Select distinct EmployeeID from Employee. Have there any convenient to throw the result of select items into a static array? or I must iterate throw the select result to add selected item into a dynamic array?
9
1588
by: Steve | last post by:
Hello, I created a structure ABC and an array of type ABC Public Structure ABC Dim str1 As String Dim int1 As Integer End Structure Public ABC1 As New ABC, ABC2 As New ABC
1
9895
by: kalyancvns | last post by:
as per the books array list is synchronized by using the method like Collections.SynchronizedList(), can any body give an example how to use this method?
22
20730
by: bela | last post by:
Hello, I am very new to JAVA. I would like to know, how to read *.csv file from java and how to save that data into an array. regards, bela
3
6124
by: raylopez99 | last post by:
I suspect the answer to this question is that it's impossible, but how do I make the below code work, at the point where it breaks (marked below). See error CS0411 This is the complete code. Copy and paste it into VS2008 for a Windows Forms application. With some effort I can do the job with a user defined function, but my question is whether I can do it using the built in library function "Array.Find" and a predicate that takes...
0
8618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8916
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8885
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7752
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.