Creating multiple nclists

Moderator: wwlytton

Post Reply
Krishna Chaitanya
Posts: 70
Joined: Wed Jan 18, 2012 12:25 am
Location: University of Pavia

Creating multiple nclists

Post by Krishna Chaitanya » Sat Mar 10, 2012 3:33 am

Hi,

Is there a way to create multiple nclists in the following way? Suppose, I want to create nclist0, nclist1....nclist100. How can I do this? Can it be done using a loop?

for i=0, 100 {
objref nclist%d
nclist%d=new List()
}

I do not know whether this is possible. Just had a thought. Can we do it by any possible means? Thank you.

ted
Site Admin
Posts: 5620
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Creating multiple nclists

Post by ted » Mon Mar 12, 2012 1:01 pm

One could
objref foo[NUMBER]
for i = 0,NUMBER-1 foo = new ClassName()
However, it is preferable to use lists to manage multiple instances of any class--including multiple instances of lists. The chief problem with using "arrays of objrefs" is that such code does not scale well--arrays have fixed size, and one always has to keep track of the actual number of objrefs in any array. This contrasts sharply with lists, which make it easy to
--add new objrefs (e.g.

Code: Select all

objref listoflists
listoflists = new List()
for i=0,NUMBER-1 listoflists.append(new List())
// later you need to add another list
listoflists.append(new List())
--discover how many objrefs are in a list
--delete any objref in the list
regardless of how many objrefs are in a particular list.

Post Reply