Results 1 to 8 of 8
  1. #1
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Don't know why the output comes out weird!

    Hi, when I run the following code, I get some weird output:

    Java Code:
    package optimalpagereplacement;
    
    import java.util.*;
    
    public class OptimalPageReplacement {
    
        public static void main(String[] args) {
     
            int pageFaultCount = 0;
            
            int[] referenceValues = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1};
            
            System.out.println("Here are the reference values: " + referenceValues);
            
            List<Integer> pageFrame = new ArrayList<Integer>();
            
            pageFrame.add(referenceValues[0]);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.add(referenceValues[1]);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.add(referenceValues[2]);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            
            
            pageFrame.set(0,2);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.set(2,3);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.set(1,4);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.set(1,0);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.set(2,1);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.set(0,7);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            System.out.println("Number of Page Faults: " + pageFaultCount);
        }
    }
    Here is the output:

    Java Code:
    Here are the reference values: [I@4e857327
    The Page Frame contains the following values: [7]
    The Page Frame contains the following values: [7, 0]
    The Page Frame contains the following values: [7, 0, 1]
    The Page Frame contains the following values: [2, 0, 1]
    The Page Frame contains the following values: [2, 0, 3]
    The Page Frame contains the following values: [2, 4, 3]
    The Page Frame contains the following values: [2, 0, 3]
    The Page Frame contains the following values: [2, 0, 1]
    The Page Frame contains the following values: [7, 0, 1]
    Number of Page Faults: 9
    Why does it say "[I@4e857327" on the first line? Isn't it just supposed to output all the numbers in the array? Am I missing something?

    Thanks for any advice/help!

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

    Default Re: Don't know why the output comes out weird!

    Why does it say "[I@4e857327" on the first line? Isn't it just supposed to output all the numbers in the array?
    No, what it prints is what it is supposed to print. The "[I" means that the thing being printed is an array of int, and the hex numbers following the "@" are a unique identifier for the array. (For details, check the Object toString() method API docs, the JLS description of the class objects associated with arrays, and the getName() method of class Class.)

    If you want to print the contents of the array, use a for loop.

  3. #3
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Don't know why the output comes out weird!

    Oh wow.. I didn't think about using a for loop.. Thanks for your help!

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Don't know why the output comes out weird!

    Or you can use the java.util.Arrays.toString() method to convert the array into a delimited string.

  5. #5
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Don't know why the output comes out weird!

    Here is the new code:

    Java Code:
    package optimalpagereplacement;
    
    import java.util.*;
    
    public class OptimalPageReplacement {
    
        public static void main(String[] args) {
     
            int pageFaultCount = 0;
            
            int[] referenceValues = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1};
            
            System.out.println("Here are the reference values: " + referenceValues);
            
            for(int a = 0; a < referenceValues.length; a++) {
                
                System.out.println(referenceValues[a]);
            }
             
            List<Integer> pageFrame = new ArrayList<Integer>();
            
            pageFrame.add(referenceValues[0]);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.add(referenceValues[1]);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.add(referenceValues[2]);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            
            
            pageFrame.set(0,2);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.set(2,3);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.set(1,4);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.set(1,0);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.set(2,1);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            pageFrame.set(0,7);
            pageFaultCount++;
            System.out.println("The Page Frame contains the following values: " + pageFrame);
            
            System.out.println("Number of Page Faults: " + pageFaultCount);
        }
    }
    Why does it still show the weird characters on the first line of the output?

    Java Code:
    Here are the reference values: [I@1b4b2db7
    7
    0
    1
    2
    0
    3
    0
    4
    2
    3
    0
    3
    2
    1
    2
    0
    1
    7
    0
    1
    The Page Frame contains the following values: [7]
    The Page Frame contains the following values: [7, 0]
    The Page Frame contains the following values: [7, 0, 1]
    The Page Frame contains the following values: [2, 0, 1]
    The Page Frame contains the following values: [2, 0, 3]
    The Page Frame contains the following values: [2, 4, 3]
    The Page Frame contains the following values: [2, 0, 3]
    The Page Frame contains the following values: [2, 0, 1]
    The Page Frame contains the following values: [7, 0, 1]
    Number of Page Faults: 9

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Don't know why the output comes out weird!

    Quote Originally Posted by Asvin View Post
    Why does it still show the weird characters on the first line of the output?

    Java Code:
    Here are the reference values: [I@1b4b2db7
    pbrockway2 already answered that in the very first response. Didn't you read it?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Don't know why the output comes out weird!

    I used a for loop, but it still shows that!

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

    Default Re: Don't know why the output comes out weird!

    I was trying to make two points: (1) System.out.println(referenceValues) will always and forever print something like "[I@4e857327". It's supposed to. and (2) If you want to display the array contents, use a for loop.

    You are using a for loop, and that's great. But you still have a line printing referenceValues and you should remove it if you don't want it.

Similar Threads

  1. Weird output from Array program
    By zniightmare in forum New To Java
    Replies: 2
    Last Post: 03-07-2012, 02:22 AM
  2. Weird array output
    By Army in forum New To Java
    Replies: 3
    Last Post: 01-17-2012, 07:58 AM
  3. Weird output
    By gandalf5166 in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 10:17 PM
  4. Seriously weird output
    By gandalf5166 in forum Java Applets
    Replies: 4
    Last Post: 02-27-2010, 05:16 AM
  5. Weird data output
    By Shaolin in forum New To Java
    Replies: 12
    Last Post: 12-11-2007, 05:27 PM

Posting Permissions

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