Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For-loop not evaluating all entries

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.

like image 373
etpt Avatar asked Aug 05 '18 22:08

etpt


1 Answers

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

like image 193
GBlodgett Avatar answered Oct 05 '22 23:10

GBlodgett