Results 1 to 8 of 8
- 05-13-2012, 09:24 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
Don't know why the output comes out weird!
Hi, when I run the following code, I get some weird output:
Here is the 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); } }
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?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
Thanks for any advice/help!
- 05-13-2012, 09:46 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Don't know why the output comes out weird!
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.)Why does it say "[I@4e857327" on the first line? Isn't it just supposed to output all the numbers in the array?
If you want to print the contents of the array, use a for loop.
- 05-14-2012, 12:39 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
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!
- 05-14-2012, 02:26 AM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 05-14-2012, 08:23 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
Re: Don't know why the output comes out weird!
Here is the new code:
Why does it still show the weird characters on the first line of the 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); 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); } }
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
- 05-14-2012, 09:15 PM #6
- 05-14-2012, 10:18 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
Re: Don't know why the output comes out weird!
I used a for loop, but it still shows that!
- 05-14-2012, 10:42 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
-
Weird output from Array program
By zniightmare in forum New To JavaReplies: 2Last Post: 03-07-2012, 01:22 AM -
Weird array output
By Army in forum New To JavaReplies: 3Last Post: 01-17-2012, 06:58 AM -
Weird output
By gandalf5166 in forum New To JavaReplies: 2Last Post: 02-28-2010, 09:17 PM -
Seriously weird output
By gandalf5166 in forum Java AppletsReplies: 4Last Post: 02-27-2010, 04:16 AM -
Weird data output
By Shaolin in forum New To JavaReplies: 12Last Post: 12-11-2007, 04:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks