5

This is my code:

import java.util.List;
import java.util.ArrayList;

public static void main(String[] args){

    List<Integer> possible = new ArrayList<Integer>();

    for(int i=0; i<1000000; i++){
        possible.add(i);
    }

    for(int i: possible){
        System.out.println(i);
    }
}

I expected the output to print values from 0 to 999,999 with spaces in between. However, my output is:

208
850209
850210
850211... all the way to 999999

Why is my for loop skipping hundreds of thousands of values? I tried replacing int with long and double. Both skip values when running. Any help? I am just trying to create an ArrayList of values from 0 to 999,999.

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
etpt
  • 53
  • 4
  • What is the output if, just for debugging, you print in the first loop, instead of the second? – Yunnosch Aug 05 '18 at 22:43
  • If you are on a unix system, pipe the output to a file and see if it is indeed skipping all those values. `java myprogram | tee output.txt` – smac89 Aug 05 '18 at 22:44

3 Answers3

5

Your program works fine. However the standard console will only display so many lines. So most of your output is getting cut off, making it appear that it is skipping thousands of numbers

Depending on what IDE you are using you should be able to change this. Go to console and change the console buffer size.

I use Eclipse so the way to change it in Eclipse is to go to window then to preferences and then under run/debug there should be a console section with the buffer size

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
0

It works, I guess it's just not printed to the Console. If you set a break point after adding all numbers and before printing, you should see that the size of the array is 100000.

0

The output console has a limit to print values.

Your List is filled correctly, if you tried to print its size you will find that it works good.

however, You can increase your console output limit

To do so, Check this answer https://stackoverflow.com/a/33800176/5335885

Mohamed Yehia
  • 400
  • 1
  • 5
  • 15