
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Increment All Elements of an Array by One in Java
Arrays are one of the most fundamental data structures in programming, and they are used to store elements of the same data type in contiguous memory locations. If you are not familiar with the array concept, do check out this article on Arrays.
In this article, we will learn how to increment every element of an array by one and print the incremented array. For example,
If the input array is
[3,7,10]
The output should be
[4,8,11]
We can solve this problem using different approaches ?
- Using for-loop
- Using java streams
- Using arrays.setAll() method
- Using recursion
Note: All the approaches have the same time complexity, which is O(n).
Using for-loop
This is a simple, easy-to-understand approach where we use a for loop to iterate through the array and add each element by one. The steps involved in this approach are ?
- Take an array as input.
- Using a for loop, iterate from the first to the last index of the array.
- Inside the loop, fetch the element at the particular index and increment the value by one.
- Now print the incremented array as output.
Implementation code
Below is the Java program where we use a for-loop to traverse the array and increment each element by one.
public class Incrementarray { public static void increment(int[] arr) { for (int i = 0; i < arr.length; i++) { arr[i]++; } } public static void main(String[] args) { int[] arr = {10, 20, 30, 40, 50}; increment(arr); System.out.println("Incremented array: " + java.util.Arrays.toString(arr)); } }
Output
Incremented array: [11, 21, 31, 41, 51]
Using java streams
In this approach we utilize Java streams to increment each element of the array. In case you are not familiar with Java streams, you can check out this article on Java Streams. This implementation involves the following steps ?
- Take an input array
- convert the array into a stream using the Arrays.stream(array) method.
- Use map(x -> x+1) operation to process each element and return the incremented value.
- Now convert the stream back to an array using toArray() function.
- Now print the output array using Arrays.toString().
Implementation code
Below is the Java program where we convert the array into a stream, apply a mapping function to increment each element, and convert it back to an array.
import java.util.Arrays; public class Incrementarray { public static int[] increment(int[] arr) { return Arrays.stream(arr).map(x -> x + 1).toArray(); } public static void main(String[] args) { int[] arr = {17, 23, 35, 42, 59}; int[] incrementedArray = increment(arr); System.out.println("Incremented array: " + Arrays.toString(incrementedArray)); } }
Output
Incremented array: [18, 24, 36, 43, 60]
Using arrays.setAll()
In this approach we use the Arrays.setAll() method to increment every element of an array in place. This approach uses the following steps ?
- Import the arrays class using java.util.Arrays to manipulate the arrays.
- Use Arrays.setall() method to iterate through the array and apply the function ' i-> arr[i]+1' to increment the original values in the array.
- Now print the array.
Implementation code
Below is the Java program where we use the Arrays.setAll() method to modify each element in the array by applying a lambda function.
import java.util.Arrays; public class Incrementarray { public static void increment(int[] arr) { Arrays.setAll(arr, i -> arr[i] + 1); } public static void main(String[] args) { int[] arr = {36,82,43,24,75}; increment(arr); System.out.println("Incremented array: " + Arrays.toString(arr)); } }
Output
Incremented array: [37, 83, 44, 25, 76]
Using recursion
In this approach we use recursion to increment each value of the array.
Implementation steps
The steps used for this method are ?
- Create a recursive method with an array and index as parameters.
- The base condition is ' index<arr.length ' which means the recursion stops when the index equals the length of the array.
- Use arr[index]++ to increment each value.
- Reursively call the same method for the next index by incrementing the index value
- Now print the modified array.
Implementation code
Below is the Java program where we use recursion to traverse the array and increment each element.
public class Incrementarray { public static void incrementRecursively(int[] arr, int index) { if (index < arr.length) { arr[index]++; incrementRecursively(arr, index + 1); } } public static void main(String[] args) { int[] arr = {72,42,32,52,92}; incrementRecursively(arr, 0); System.out.println("Incremented array : " + java.util.Arrays.toString(arr)); } }
Output
Incremented array : [73, 43, 33, 53, 93]