ArrayList blues...

  • Thread starter Thread starter Tim
  • Start date Start date
T

Tim

Hi,

I am creating a printing engine and I am cycling though my recordset. Each
item gets added to an ArrayList. Once there are more items that will fit on
the page I add the item ArrayList to a pages ArrayList. Seems pretty simple
so far.

However, when I clear the items ArrayList it clears everything. What I am
trying to do is add a page worth of items to the pages ArrayList, clear it
and then create another page worth of items etc...

Can someone help me?

This is the code. Pages and PageItems are both ArrayList.

[code to add to page items]
this.Pages.Add(this.PageItems);

this.PageItems.Clear();

[loop]
 
Tim said:
Hi,

I am creating a printing engine and I am cycling though my recordset. Each
item gets added to an ArrayList. Once there are more items that will fit
on the page I add the item ArrayList to a pages ArrayList. Seems pretty
simple so far.

However, when I clear the items ArrayList it clears everything. What I am
trying to do is add a page worth of items to the pages ArrayList, clear it
and then create another page worth of items etc...

Can someone help me?

This is the code. Pages and PageItems are both ArrayList.

[code to add to page items]
this.Pages.Add(this.PageItems);

this.PageItems.Clear();


Try to set PageItems to new ArrayList().

By calling this.Pages.Add(this.PageItems) you are merely adding a reference
to the underlying items-arraylist, you then clear the same arraylist. If
you set the items arraylist to new ArrayList you a creating a reference to a
new arraylist, i.e. the previous one still exists with all the items in
it...

 
From your description

ArrayList pageItems = new ArrayList(); (A)
ArrayList pages = new ArrayList();

const int PAGE_LENGTH = 24;
int counter = 0;
while (true) {

String item = GetNextItem();
counter++;
if (item == null) {
pages.Add(pageItems);
break;
}
pageItems.Add(item);
if (pageItems.Count == PAGE_LENGTH) {
pages.Add(pageItems);
pageItems.Clear();
(B)
counter = 0;
}

}


Problem.
You only ever create a single pageItems arraylist (A).
You add the items to that list, add that list to the page and then
clear it.
Repeat

At the end you should have a set of empty pageItems + the last page.

You need to create a new pageItems list for each page

Replace the Clear() call (B) with

pageItems = new ArrayList();


hth,
Alan.
 
Hi,

Easy, do not clear the Arraylist, simply create a new one:

[code to add to page items]
this.Pages.Add( PageItems);

PageItems = new ArrayList();

[loop]

The previous one will be kept in the pages ArrayList so it will not be GCed

cheers,
 
Thank you Ignacio, that works!

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

Easy, do not clear the Arraylist, simply create a new one:

[code to add to page items]
this.Pages.Add( PageItems);

PageItems = new ArrayList();

[loop]

The previous one will be kept in the pages ArrayList so it will not be
GCed

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Tim said:
Hi,

I am creating a printing engine and I am cycling though my recordset.
Each item gets added to an ArrayList. Once there are more items that will
fit on the page I add the item ArrayList to a pages ArrayList. Seems
pretty simple so far.

However, when I clear the items ArrayList it clears everything. What I am
trying to do is add a page worth of items to the pages ArrayList, clear
it and then create another page worth of items etc...

Can someone help me?

This is the code. Pages and PageItems are both ArrayList.

[code to add to page items]
this.Pages.Add(this.PageItems);

this.PageItems.Clear();

[loop]
 
Thank you Alan, that was it!

ArrayList pageItems = new ArrayList(); (A)
ArrayList pages = new ArrayList();

const int PAGE_LENGTH = 24;
int counter = 0;
while (true) {

String item = GetNextItem();
counter++;
if (item == null) {
pages.Add(pageItems);
break;
}
pageItems.Add(item);
if (pageItems.Count == PAGE_LENGTH) {
pages.Add(pageItems);
pageItems.Clear();
(B)
counter = 0;
}

}


Problem.
You only ever create a single pageItems arraylist (A).
You add the items to that list, add that list to the page and then
clear it.
Repeat

At the end you should have a set of empty pageItems + the last page.

You need to create a new pageItems list for each page

Replace the Clear() call (B) with

pageItems = new ArrayList();


hth,
Alan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top