Array input in java in 2021 | codeYourslf - coding in mobile

How to take integer inputs and store it aslo print using array in java for beginners. I will code using mobile phone | codeYourslf

Array input in java

Array input in java in 2021 | codeYourslf - coding in mobile

The source code of this program is given below 👇

How to take array input in java

- We all know array can store multiple elements of same data type, like integer, string, char etc. I mean if I have declared a integer array, then in that particular array I can only store integer numbers(1,4,-5,8 etc.) not any name or any alphabet.

array initialisation

- Let's you want to store 5 integer value, first of all you have to declare a array of integer type and the size of the array should be 5.

         int[] arrayName = new int[5];

- In place of arrayName you can put your variable, I mean you can put the array name as you wish.

" If you are more comfortable with video lecture then I have uploaded a video regarding this topic i.e how to take input in array in java, you can check out that CLICK HERE.

 add value to initialised array

- The array i.e arrayName is ready to receive integer inputs.

- Here you can give integer value to the array by using the for loop and by using Scanner class it will store value in the memory.

     for(int i = 0; i < 5; i++)

          {

                arrayName[i] = sc.nextInt();

           }

- We know the array index starts from 0, so here we have started the value of i from 0 and it will goes upto 4. It means it will cover the 5 block declared by the array and insert the respective value to each block.

Array input in java in 2021 | codeYourslf - coding in mobile

"I am doing my coding in mobile phone, If you are intrested to see how do I code on phone then CLICK HERE."

How to display value in array in java

Array input in java in 2021 | codeYourslf - coding in mobile

- Do you know "arrayName[0]" will return the value that is inside the 0th index of the array.

Here System.out.println(arrayName[0]);  will be 7

- By doing this arrayName[1], arrayName[2] ...... arrayName[n], you can able to get the value that are inside the respective index.

- But the problem is, if there is 100 block(the size of array is 100), then we have to write 100 lines of System.out.println(); which is not good for a programmer.

- So you can use foor loop to solve this problem, how see

    for(int i = 0; i < 5; i++)

 {

      System.out.println(arrayName[i]);

 }

- Here for the first time the value of i is 0, so when it enter to the code the code become ..

System.out.println(arrayName[0]); return the value at index 0 i.e 7.

- Now the 'i' will be incremented by 1 and 'i' become 1 now, so the will be System.out.println(arrayName[1]) System.out.println(arrayName[1]); which will return the value at index 1 and so on it goes upto index 4 and will return all the values inside the array.

understood this is the whole explanation for the topic how to take input in array.

source code for array input in java

 
package CodeYourslf.arrayProblem;

import java.util.Scanner;

public class arrayInput {

  public static void main(String args[]) {

    Scanner sc = new Scanner(System.in);

    

    System.out.println("Enter the size of your array: ");

    int size = sc.nextInt();

    int[] arr = new int[size];

    System.out.println("Enter the elements for the array: ");

    for (int i = 0; i < size; i++) {

      arr[i] = sc.nextInt();

    } 

    System.out.println("The array elements are: ");

    for (int i = 0; i < size; i++) {

      System.out.println(arr[i]);

    }

  }

}


 

Output screenshot here:-

Array input in java in 2021 | codeYourslf - coding in mobile


Video Explanation for array input in java 





`

Post a Comment

Hey Please 👏, feel free to share your opinion 🌍
© codeYourslf. All rights reserved. Distributed by Pixabin