Thursday 4 January 2018

java - System out println

itemprop="text">

I'm printing 2 lines to the console.
They both print, but when the second one has printed, the first one changes to the
second one, so the 2 lines are identical. I've never encountered this before. Why does
the second print overwrite the first one, and how do I fix it?



public static void main(String
args[]){
new MergeSort(90000);



System.out.println("Array to be mergesorted: "
+Arrays.toString(array));

long start =
System.currentTimeMillis();

mergeSort(array, 1,
array.length);

long end =
System.currentTimeMillis();

System.out.println("Result: " +
Arrays.toString(array)
);

}


The
constructor:



public MergeSort(int
n){
Random rand = new Random();
array = new int[n];

for(int i = 0; i array[i] =
rand.nextInt(101);


}
}


Rest of
code:



public static void
merge(int[] A, int p, int q, int r){
//
//length of subarray
1
int n1 = q-p+1;


//length of subarray
2
int n2 = r-q;

int[] L = new int[n1+1];

int[] R = new int[n2+1];

for(int i = 0; i < n1;
i++){
L[i] = A[p+i-1];
}


for(int
j=0; j< n2; j++){
R[j] = A[q+j];
}

L[n1]
= Integer.MAX_VALUE;
R[n2] = Integer.MAX_VALUE;

int i =
0;
int j = 0;


for(int k = p-1; k < r;
k++){
if(L[i] <= R[j]){
A[k] = L[i];
i++;

}
else{
A[k] = R[j];
j++;

}

}

}

public static void
mergeSort(int[] A, int p, int r){
if (p int q = (int)
Math.floor((r+p)/2);
mergeSort(A, p, q);
mergeSort(A, q+1,
r);
merge(A, p, q, r);


}
}

class="post-text" itemprop="text">
class="normal">Answer



This is
due to the buffer limit of the console in your IDE. I cannot explain why exactly you are
seeing duplicate output of certain strings other than to say it looks like theres a bug
in how it clears out old characters in the buffer when it hits the
limit.



I think Eclipse comes with a default of
80,000 character limit in its console output. Since you are printing 90,000 numbers
between 1-100 two times that means your over shooting this buffer and then
some.



To increase the buffer limit on the
console:





  • Right
    click on the output window in Eclipse and select
    Perferences

  • Change "Console buffer size (characters)" to
    be your desired
    limit.



Ideally would
change it to something higher than the maximum characters your printing out for this
program. Maybe something like 800,000?



/>

Heres a picture of the preferences
window.

rel="nofollow noreferrer"> alt="enter image description
here">



Edit:
This question reminded me of href="https://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing">another
interesting question in which the answer to the question lied inside how the
word wrapping was performed in the terminal output. Not really the same as this question
but it is related and quite an interesting question/answer. Its worth a read and there's
definitely a lesson to be learned in all of this.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...