---
# System prepended metadata

title: 1D Arrays_2

---

# 1D Arrays_2

## Inserting at an index in Array
 Given an array and element X. We have to update our array at index Y with value X.
Example: arr of size 4 given. Insert 89 at 2nd index.

Code :
```java
int ar[] = {33,3,4,5};
ar[2] = 89;
System.out.println(ar[2]);
```

Output :
```
89
```

Explanation :
```
Value at index 2 has been updated with 89. Updated array: {33, 3, 89, 5}
```

## Swapping elements in an array

Given an array and two indexes x, y. Swap elements of index X and Y.
Example: Array = {33,3,4,5}. Swap element at index 2 with index 1 element. Final array = {33, 4, 3, 5}.

Code :
```java
int ar[] = {33,3,4,5};
int temp = ar[2];
ar[2] = ar[1];
ar[1] = temp;
```

Final Array:
```
{33, 4, 3, 5}
```

Explanation:
```
Index 1 element swapped with index 2 element.
```

## Printing elements of a given range of indices in an array
Given an array. Print all the elements in given range of indices from x to y both inclusive.
Example: ar = {33,3,4,5,6,7}. Print elements between index 2 to 4 both inclusive. 

Code :
```java
int ar[] = {33,3,4,5,6,7};
for (int i = 2; i <= 4; i++) {
    System.out.println(ar[i]);
}
```

Output :
```
4
5
6
```

Explanation :
```
Here all the elements between index 2 to index 4 are printed.
```