Don't know why the output comes out weird!
Hi, when I run the following code, I get some weird output:
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:
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!
Re: Don't know why the output comes out weird!
Quote:
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.
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!
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.
Re: Don't know why the output comes out weird!
Here is the new code:
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?
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
Re: Don't know why the output comes out weird!
Quote:
Originally Posted by
Asvin
Why does it still show the weird characters on the first line of the output?
Code:
Here are the reference values: [I@1b4b2db7
pbrockway2 already answered that in the very first response. Didn't you read it?
db
Re: Don't know why the output comes out weird!
I used a for loop, but it still shows that!
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.