System out println

I am printing 2 lines to the console. They both print, but when the second is printed, the first changes to the second, so the two lines are identical. I have never encountered this before. Why does the second seal overwrite the first and how to 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) ); } 

Constructor:

 public MergeSort(int n){ Random rand = new Random(); array = new int[n]; for(int i = 0; i <array.length; i++){ array[i] = rand.nextInt(101); } } 

The rest of the 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 = rq; 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<r){ int q = (int) Math.floor((r+p)/2); mergeSort(A, p, q); mergeSort(A, q+1, r); merge(A, p, q, r); } } 
+5
source share
1 answer

This is due to the console buffer limitation in your IDE. I can’t explain why exactly you see a duplicate of the output of certain lines, except to say that it looks like an error in how it clears old characters in the buffer when it reaches the limit.

I think Eclipse has a default limit of 80,000 characters in its console release. Since you print 90,000 numbers from 1 to 100 times, this means that you iterate over this buffer, and then some.

To increase the buffer limit on the console:

  • Right-click on the output window in Eclipse and select "Tutorials"
  • Change "Console Buffer Size (characters)" to be your desired limit.

Ideally, this will change it to something more than the maximum characters that you print for this program. Maybe something like 800,000?


Here is an image of the settings window. enter image description here

Edit: This question reminded me of another interesting question , in which the answer to the question went into the question of how the words were packed into the terminal output. Not exactly the same as this question, but it is related and a rather interesting question / answer. It is worth reading, and there is definitely a lesson that needs to be learned in all of this.

+5
source

Source: https://habr.com/ru/post/1244548/


All Articles