What are Arrays?

Array is a data structure that stores multiple values in a single variable. You can say that a Array is a collection of “same type” of elements in a given specific length . In simple words arrays are like a list with stores a specific limited values For eg:

String[] arr = {mike,roy,mangesh};

The Important thing about Array is, you need to define the length of the Array for eg: In the below eg the length of the array is 9. And in array the counting of numbers start’s from 0.

int[] arr = new int[9]; 

Now you know, how to declare a array. Now lets see how to add elements to the a array.

String[] arr = new String[2];
arr[0] = "jackson";
arr[1] = "will simth";

Now how to access a array elements? You can access in Three ways:

  • 1 simple method
String[] arr = new String[2];
arr[0] = "jackson";
arr[1] = "will simth";
arr[0]
System.out.println(arr[0]); // output will be "jackson"
  • 2 for loop
for(int i=0;i<arr.length;i++){
   System.out,println(arr[i]);
}

// output be like
// jackson
// will simth
  • 3 for each loop
for(String i: arr){
   System.out.println(i);
}

How to change an Array element.

To change a array element, you need to specific element location for eg:

String[] arr = {1,2,3,4,5};
arr[3] = 10;
// output will be 1,2,3,10,5

Thanks for reading, this is just a short intro to Arrays.

Leave a comment