Page 1 of 1

Creating multiple nclists

Posted: Sat Mar 10, 2012 3:33 am
by Krishna Chaitanya
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.

Re: Creating multiple nclists

Posted: Mon Mar 12, 2012 1:01 pm
by ted
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.