Results 1 to 6 of 6
  1. #1
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default 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?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Int Array Initialization Problem

    I need these to be initialized to null but I know you can't assign a primitive array to null.
    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.

    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".

  3. #3
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Int Array Initialization Problem

    Quote Originally Posted by pbrockway2 View Post
    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.

    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".
    Thanks! So I just have to set the array to null rather than the elements. It always seems to be something simple that causes my issues!

  4. #4
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default 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?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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.

    ** This space for rent **

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Int Array Initialization Problem

    That's entirely down to how you've written your code.
    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.

Similar Threads

  1. Initialization Problem of CheckBoxes
    By fatabass in forum AWT / Swing
    Replies: 2
    Last Post: 02-11-2012, 12:19 AM
  2. An Array of Objects (Class Initialization)
    By Chris_C in forum New To Java
    Replies: 9
    Last Post: 02-09-2011, 05:49 AM
  3. Array initialization difference
    By sln69 in forum Advanced Java
    Replies: 2
    Last Post: 10-10-2008, 02:38 AM
  4. 2D Array Initialization
    By M77 in forum Advanced Java
    Replies: 3
    Last Post: 06-04-2008, 02:12 PM
  5. initialization value problem
    By ravian in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 10:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •