# 1D Arrays_1 ## What are arrays? > Array is a collection of same DataType values/variables. > By default a new integer array is created with all elements as 0. ## How to create an array? 1. datatype name[] = new datatype[size] Code1 : ```java int ar[] = new int[5] ``` Explanation : ``` Integer array of size 5 in name of ar created ``` 2. datatype []name = new datatype[size] Code2 : ```java int []ar = new int[5] ``` Explanation : ``` integer array of size 5 in name of ar created ``` 3. datatype[] name = new datatype[size] Code3 : ```java int[] ar = new int[5] ``` Explanation : ``` integer array of size 5 in name of ar created ``` 4. We can directly create an array by giving values Code4 : ```java int ar[] = {33,3,4,5,6,7}; ``` Explanation : ``` A new array of size 6 is created with elements: {33,3,4,5,6,7} ``` Note: We can create array using any of the above methods. ## Indexing in an array > Indexing in an array starts from 0 and goes till N - 1 (N being length of array). It means index of first element is 0 and index of last element is N - 1. 1. We access these indexes by following syntax: array_name[index number] Code1 : ```java int ar[] = new int[5]; System.out.println(ar[0]); ``` Output: ``` 0 ``` Explanation : ``` By default all elements in a new array are 0. ``` 2. Index out of Bound : Code2 : ```java int ar[] = new int[5]; System.out.println(ar[6]); ``` Output : ``` Error: ArrayIndexOutOfBoundsException ``` Explanation : ``` In above array it does not have index 6. ``` 3. Finding length of an array (array_name.length): Code3 : ```java int ar[] = new int[5]; System.out.println(ar.length); ``` Output : ``` 5 ``` Explanation : ``` We have total 5 elements in this array. ``` 4. We can store values using indexes: Code4 : ```java int ar[] = new int[4]; ar[0] = 12; ar[1] = 34; ar[2] = 2; ar[3] = 21; ``` Output : ``` Final array: {12, 34, 2, 21} having size 4. ``` ## Iterating over an array to access/print values: 1. Lets say we have an array of size 3: ar[3] = {4, 3, 5}. Then we can acces these values using index: Code : ```java System.out.print(arr[i]) ``` Output : ``` It will print value at ith index ``` 2. We are printing values of an array one by one using indices. Code : ```java System.out.print(arr[0]) // It will print -> 4 System.out.print(arr[1]) // It will print -> 3 System.out.print(arr[2]) // It will print -> 5 ``` 3. Let us write a function that prints all the elements of a given array. Code : ```java static void printArray(int[] a){ for(int i = 0; i < a.length; i++){ System.out.println(a[i]); } } public static void main(String args[]){ int[] arr = {33, 3, 4, 5}; printArray(arr); } ``` Output : ``` 33 3 4 5 ``` Explanation : ``` In above code we are passing our array to a function to iterate in the given array. ``` 4. We can create array by taking values as input from user. Code : ```java Scanner sc = new Scanner(System.in); int ar[] = new int[5]; for (int i = 0; i < 5; i++) { ar[i] = sc.nextInt(); } ``` Input: ``` 1 2 3 4 5 ``` Explanation : ``` ar = {1, 2, 3, 4, 5} is created. ``` ## Creating a new array using an old array name : Code : ```java int ar[] = new int[3]; ar[0] = 10; ar[1] = 20; ar[2] = 30; ar = new int[4]; System.out.print(ar[0]); ``` Output : ``` 0 ``` Explanation : ``` If we use existing array's name to create new array then that name will be allocated to new array only. ```