Filling an array. How to pass the size of the array through Run configurations -> Arguments?

int[] c; Random rnd = new Random(); for (int i = 0; i < c.length; i++) { c[i] = rnd.nextInt(100); } 

    2 answers 2

    Argument transfer the size of the array, parse this value from string to integer, and use when declaring a new array.

      int len = Integer.parseInt(args[0]); int[] c = new int[len]; Random rnd = new Random(); for (int i = 0; i < c.length; i++) { c[i] = rnd.nextInt(100); } 

      In order to pass arguments to the Main () method using Eclipse or Intelij IDE, you need to go to the drop-down list of the Run Main icon. Select Run Configurations -> Arguments -> Program arguments - here you enter the lines separated by a space. Next we need to process the arguments:

       public static void main(String[] args){ int size = Integer.parseInt(args[0]); int[] array = new int[size]; } 

      That's it, to convert a string into a number, we use the static method of the Integer class parseInt.