Forums Register Login

Array index

+Pie Number of slices to send: Send
Hello,

I'm hoping someone can help me wrap my head around this:
1. I'm trying to set a variable to point to the last full cell of an array and then create a new larger array and then have the variable be updated to point to the new array's last full cell index value.
a) Should the variable be declared static?
b) What would be the best initial value to set this variable to? Shouldn't it correspond to a <for> loop index rather than a solid integer?

c) How would you update <lastfullcell> because if you passed the index through a method's loop index isn't the array index value garbage collected after the method completes?

+Pie Number of slices to send: Send
What determines if a cell is "full" or not? For an array of primitives, every element is set to 0 - can you tell if this is empty or full with a legitimate value of 0?

For an array of Objects, you could check for null references I suppose, but even that may not be what you want. If the 5th - 7th are null, and the 8th has something, does that matter? if not, you would have to start at the end and work backwards to find the last populated element...
+Pie Number of slices to send: Send
 

Greg Murphy wrote:
a) Should the variable be declared static?


Probably not. static means one instance per class. You want a copy for all objects, don't you?


b) What would be the best initial value to set this variable to? Shouldn't it correspond to a <for> loop index rather than a solid integer?


Think about where you want to keep the contents of an array element or an index to that element. Which is going to be easier to manipulate?


c) How would you update <lastfullcell> because if you passed the index through a method's loop index isn't the array index value garbage collected after the method completes?


Not if there's still a reference to it. And where is this method going to reside? In a utility class or in the expanding array class itself?
+Pie Number of slices to send: Send
Hi,
What do you mean by 'last full cell'? Last filled
cell?
+Pie Number of slices to send: Send
Thanks for the responses. I appreciate the help.
I still am a little foggy though.

In reply to Mr. Rosenberger:
As far as the last full cell - I'm talking about the last cell that has data in it although the array would be much larger.

In reply to Mr. Snortum:
I definitely don't care about the array element only the array index.
I don't understand the answer to c) -
Say I have an expandingArray method - how would I update <lastfullcell>'s index after a <new> larger array was created?


I forget to mention that in the newer array I would have added another element to increase its index size.
+Pie Number of slices to send: Send
If you make a new array to expand the old array, I assume you will pass it back, probably as a return value. The return value is not the object itself but a reference to it. The caller will set this reference to some variable, I assume. The GC will not touch an object that has a reference.
+Pie Number of slices to send: Send
 

Greg Murphy wrote:I'm hoping someone can help me wrap my head around this:
1. I'm trying to set a variable to point to the last full cell of an array and then create a new larger array and then have the variable be updated to point to the new array's last full cell index value.


Then you have to decide what "full" means (and that was the point of fred's post).

a) Should the variable be declared static?


No.

b) What would be the best initial value to set this variable to? Shouldn't it correspond to a <for> loop index rather than a solid integer?


That's a procedural point, and entirely up to you. What do you want it to contain? The number of "full" elements? The index of the last "full" element?
THAT will determine what it should be initialised to.

About the only thing that might be worth considering is that if you choose the latter, what should it be when there are NO "full" elements?

How would you update <lastfullcell> because if you passed the index through a method's loop index isn't the array index value garbage collected after the method completes?


I suspect you're obsessing about things that don't really matter. It's your program, so you get to choose. Answer the first few questions, and I suspect you'll be much closer to a model that you can live with (and DOCUMENT ).

Winston
+Pie Number of slices to send: Send
Are you populating the array? If so, perhaps you could keep a pointer that keeps track of where the next element goes. If you remove elements, it gets trickier, but is still possible.

But basically, you create your array, and set your insertHere variable to 0. Then, when you insert an element, you do it at myArray[insertHere], and increment insertHere. You would need to check to make sure insertHere never gets bigger than the size of your array. If it does, you would then need to build your new array of the larger size.

I assume this is an academic exercise, since there are already structures that will grow as needed...
+Pie Number of slices to send: Send
Yes main (not created yet) will populate the array ultimately but I'm working on an array that is set to receive elements but is empty.

the [insert here] description could be what I need or relatively close as I think if I understand it the variable <inserthere> will be set to a number which will
be passed to the methods. Then I increase the pointer myArray[insert here++] to point at the new element(s) added into the array.

+Pie Number of slices to send: Send
Post some working code, and use code tags. That's going to help us help you.
Paper jam tastes about as you would expect. Try some on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 643 times.
Similar Threads
constructor for a class implementing doubly-linked list
Stuffing one array into another
How can I remove objects from an array?
JTable Trouble
Dynamic scrolling
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:43:09.