Skip to main content

Arrays

Arrays | Java: Arrays are using for storing the multiple same data type values.

Syntax:                                      int[] a = {1, 2, 3};
                                                  int[] b = new int[n];  where n is the size of an array.

Here is the Code:

  1. import java.io.*;
  2. class Main {
  3. public static void main (String[] args) {
  4. int[] a = {1, 2, 3};
  5. System.out.println(a[2]);
  6. for(int i=0;i<a.length;i++)
  7. {
  8.     System.out.println(a[i]);
  9. }
  10. int[] b = new int [5];
  11. for(int i=0;i<b.length;i++)
  12. {
  13.     b[i]=5;
  14.     System.out.println(b[i]);
  15. }
  16. }
  17. }

Output:
3
1
2
3
5
5
5
5
5

Here is the Video: