Array: Initialize an array with input values from the user

Q) Initialize an array with input values from the user

Example:

Enter 10 elements of the array:
1 2 3 4 5 6 7 8 9 10

import java.util.Scanner;

public class ArrayInputSample {
  
  public void enterArray () {
    Scanner sc= new Scanner (System.in);
    int [] sampleArray= new int[10]; //declaration, creation, and assignment of array reference to a array variable
    
    for (int i=0; i<sampleArray.length; i++){
      sampleArray[i]= sc.nextInt(); // initialiing each element of the array
    }
    
  }
}

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close