Posts

Showing posts with the label Part D : JAVA Arrays

Java Arrays

Image
Java Arrays  Array is a collection of similar data-types . Also  Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.   ➤ Declaring an Array To declare an array, define the variable type with  square brackets :  Syntax :- dataType varName [] = { // array elements separated by comma }; Here I am declaring an array of strings. String[] animals ; As we have declared an array , lets give values to it. String[] animals = {" Dog ", " Elephant ", " Fox ", " Cow "}; ➤ Index of an Array Index of an Array starts from zero. We also can declare an array of a given size. Here I declared an array of animals of length 1 i.e It would have 2 containers , 1 at index 0 and other at index 2. ➤ Change an Array element You can rewrite or replace an array element by declaring it again at the same index. public class MyClass { public static void main(String[] args) { S...