📜  Java程序将数组的所有元素加一

📅  最后修改于: 2022-05-13 01:55:19.966000             🧑  作者: Mango

Java程序将数组的所有元素加一

给定数组,任务是将数组的每个元素递增 1。 递增数组的所有元素需要完全遍历。完成给定任务的优化方法是具有 O(N) 的时间复杂度。

例子:

Input : arr1[] = {50, 25, 32, 12, 6, 10, 100, 150}
Output: arr1[] = {51, 25, 33, 13, 7, 11, 101, 151}

Input : arr2[] = {3, 6, 8, 12, 45}
Output: arr2[] = {4, 7, 9, 13, 46}

1. 使用预定义值:

首先,使用预先定义的值初始化数组 arr[],然后计算数组的长度。然后使用循环,在for循环的帮助下执行将值一一递增的操作。之后,上述操作的结果将存储到数组中,然后在for循环的帮助下将输出一一显示。

Java
// Java Program to Increment All
// Element of an Array by One
public class Increment {
    public static void main(String[] args)
    {
        // assigning values in array
        int[] arr = { 50, 25, 32, 12, 6, 10, 100, 150 };
        
        // finding the length of the array
        // and assigning it to the variable n
        int n = arr.length;
  
        System.out.println(
            "******* BEFORE INCREMENT *******");
        
        // printing the elements of array
        // before performing operation
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i]);
            System.out.print(" ");
        }
  
        // incrementing the values of array
        // by 1 with the help of loop
        for (int i = 0; i < n; i++) {
            arr[i] = arr[i] + 1;
        }
  
        System.out.println(" ");
        
        // elements of the array after increment
        System.out.println(
            "******* AFTER INCREMENT *******");
        
        // printing the elements of array after operation
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i]);
            System.out.print(" ");
        }
    }
}


Java
// Java Program to Increment All
// Element of an Array by One
import java.util.Scanner;
  
public class Increment2 {
  
    public static void main(String[] args)
    {
        System.out.println(
            "Enter the length of the array: ");
        
        // The object of scanner class is created
        // in order to take the input from user
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        
        // Initializing the array of size that
        // is given by the user
        int[] arr = new int[n];
        
        // Calculating the length of the array
        int l = arr.length;
  
        System.out.println(
            "Enter the values you want to insert in array: ");
        
        // Taking the values from user
        for (int i = 0; i < l; i++) {
            arr[i] = sc.nextInt();
        }
  
        System.out.println(
            "******* BEFORE INCREMENT *******");
        
        // The elements of array before increment
        for (int i = 0; i < l; i++) {
            System.out.print(arr[i]);
            System.out.print(" ");
        }
  
        // Incrementing the values of the array by 1
        for (int i = 0; i < l; i++) {
            arr[i] = arr[i] + 1;
        }
  
        System.out.println(" ");
        System.out.println(
            "******* AFTER INCREMENT *******");
        // Elements of array after increment
        for (int i = 0; i < l; i++) {
            System.out.print(arr[i]);
            System.out.print(" ");
        }
    }
}


输出
******* BEFORE INCREMENT *******
50 25 32 12 6 10 100 150  
******* AFTER INCREMENT *******
51 26 33 13 7 11 101 151 

时间复杂度: O(N),其中 N 是数组的大小

2. 具有用户定义的值:

用用户定义的值初始化数组arr[],即数组的值将由用户给出,然后计算数组的长度。然后使用循环,在for循环的帮助下执行将值一一递增的操作。之后,上述操作的结果将存储到数组中,然后在for循环的帮助下将输出一一显示。

Java

// Java Program to Increment All
// Element of an Array by One
import java.util.Scanner;
  
public class Increment2 {
  
    public static void main(String[] args)
    {
        System.out.println(
            "Enter the length of the array: ");
        
        // The object of scanner class is created
        // in order to take the input from user
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        
        // Initializing the array of size that
        // is given by the user
        int[] arr = new int[n];
        
        // Calculating the length of the array
        int l = arr.length;
  
        System.out.println(
            "Enter the values you want to insert in array: ");
        
        // Taking the values from user
        for (int i = 0; i < l; i++) {
            arr[i] = sc.nextInt();
        }
  
        System.out.println(
            "******* BEFORE INCREMENT *******");
        
        // The elements of array before increment
        for (int i = 0; i < l; i++) {
            System.out.print(arr[i]);
            System.out.print(" ");
        }
  
        // Incrementing the values of the array by 1
        for (int i = 0; i < l; i++) {
            arr[i] = arr[i] + 1;
        }
  
        System.out.println(" ");
        System.out.println(
            "******* AFTER INCREMENT *******");
        // Elements of array after increment
        for (int i = 0; i < l; i++) {
            System.out.print(arr[i]);
            System.out.print(" ");
        }
    }
}

输出:

时间复杂度: O(N),其中 N 是数组的大小