Increase Array Size in Java

Array increment in java. This is a Java program that prints the incremented array and increments each element of the array by one. Enter the array’s size, followed by all of the array’s elements. Using the for loop, we can now increment all of the array’s elements. Here is the Java source code for incrementing every element of an array by one and printing the incremented array.

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, i = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter no of elements you want in array");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements");
for(i = 0; i < n; i++)
{
a[i] = s.nextInt();
a[i]++;
}
System.out.print("Elements of array after increment by 1");
for(i = 0; i < n - 1; i++)
{
System.out.print(a[i]+",");
}
System.out.print(a[n-1]);
}
}

 

Output:

Enter no of elements you want in array2
Enter all the elements
45
34
Elements of array after increment by 146,35

Increment Operator for Array in Java

To modify the size, build a new array of the appropriate size, copy elements from the old array to the new array, then utilise the new array. Arr can only hold int values in our case. In contrast to ArrayList, which can only carry object values, arrays can hold primitive values.

s.nextint() in Java

function nextInt() As an int, scans the next token of the input. An invocation of this method of the type nextInt() acts precisely the same as an invocation of nextInt(radix), where radix is the scanner’s default radix.

Increment Operator in Java

The increment unary operator in Java raises the value of a variable by one, and the decrement unary operator reduces the value of a variable by one. Both change the operand’s value to its new value. The unary operators increment and decrement have two forms: prefix and postfix.