Results 1 to 6 of 6
Thread: Int Array Initialization Problem
- 03-28-2012, 05:38 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 46
- Rep Power
- 0
Int Array Initialization Problem
I am writing a virtual memory simulator based on Demand Paging program using the FIFO, OPT and LRU algorithms. Anyway the program is very close to working properly and I have identified the problem but not sure how to fix it. I am using int array's to simulate the pages and virtual frames and both arrays are initialized to 0 and that is where my problem comes from. I need these to be initialized to null but I know you can't assign a primitive array to null. The FIFO and LRU algorithms work perfectly if I do not use 0 to represent any of the pages.
So basically I need to be able to initialize and also "reset" the int arrays to "empty." Any way of doing this?
- 03-28-2012, 06:29 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
Re: Int Array Initialization Problem
I'm not completely sure what the "these" are: you *can* assign a variable of type int[] the value null, arrays are objects. What you can't do is assign any of its elements the value null as the elements are all of type int.I need these to be initialized to null but I know you can't assign a primitive array to null.
If the latter is what you're trying to do you could use Integer[] arrays. Elements of such arrays can be null and you might want to give that the meaning "unassigned".
- 03-28-2012, 03:43 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 46
- Rep Power
- 0
- 03-28-2012, 03:47 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 46
- Rep Power
- 0
Re: Int Array Initialization Problem
Well it seems I am going to have to try the Integer arrays and set each element to null. I tried just setting the int array to null but that just resulted in a nullpointerexception. Will I have the same issue if I change the int arrays to Integer arrays and set all the elements to null?
- 03-28-2012, 04:16 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Int Array Initialization Problem
That's entirely down to how you've written your code.
If you've written in on the assumption that none of the elements are null then you are likely to hit an NPE at some point, without a rewrite.Please do not ask for code as refusal often offends.
- 03-28-2012, 09:54 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
Re: Int Array Initialization Problem
I agree. There's not a lot to choose from between int[] and Integer[]. It's the code that does things with these data structures that needs to be looked at - the code that uses them as part of a FIFO queue or whatever.That's entirely down to how you've written your code.
Similar Threads
-
Initialization Problem of CheckBoxes
By fatabass in forum AWT / SwingReplies: 2Last Post: 02-11-2012, 12:19 AM -
An Array of Objects (Class Initialization)
By Chris_C in forum New To JavaReplies: 9Last Post: 02-09-2011, 05:49 AM -
Array initialization difference
By sln69 in forum Advanced JavaReplies: 2Last Post: 10-10-2008, 02:38 AM -
2D Array Initialization
By M77 in forum Advanced JavaReplies: 3Last Post: 06-04-2008, 02:12 PM -
initialization value problem
By ravian in forum New To JavaReplies: 2Last Post: 01-28-2008, 10:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks