I'm trying to create a program that prompts the user for a number N. This number will be used to prompt the user N times to provide an integer.
How do I create a user defined array size?
Collapse
X
-
Tags: None
-
Try This Code
Code:import java.util.Arrays; import java.util.Scanner; public class PopulatingAnArray { public static void main(String args[]) { System.out.println("Enter the required size of the array :: "); Scanner s = new Scanner(System.in); int size = s.nextInt(); int myArray[] = new int [size]; System.out.println("Enter the elements of the array one by one "); for(int i = 0; i<size; i++) { myArray[i] = s.nextInt(); } System.out.println("Contents of the array are: "+Arrays.toString(myArray)); } }Comment
Comment