HackerRank Java- 1D Array



Write the following code in the unlocked portion of your editor:

Create an array, a, capable of holding n integers.
Modify the code in the loop so that it saves each sequential value to its corresponding location in the array. For example, the first value must be stored in a0, the second value must be stored in a1, and so on.



  1. import java.util.*;
  2. public class Solution {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. int n = scan.nextInt();
  6. int[] a = new int[n];
  7. for (int i = 0; i < n; i++) {
  8. a[i] = scan.nextInt();
  9. }
  10. scan.close();
  11. // Prints each sequential element in array a
  12. for (int i = 0; i < a.length; i++) {
  13. System.out.println(a[i]);
  14. }
  15. }
  16. }

download android app