• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array index  RSS feed

 
Greg Murphy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What do you mean by 'last full cell'? Last filled
cell?
 
Greg Murphy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Greg Murphy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post some working code, and use code tags. That's going to help us help you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!