首页 > Java > java教程 > JAVA程序将整数数组的元素替换为其他元素的乘积

JAVA程序将整数数组的元素替换为其他元素的乘积

WBOY
发布: 2023-09-06 09:33:06
转载
860 人浏览过

JAVA程序将整数数组的元素替换为其他元素的乘积

整数数组是指所有元素都是整数类型的数组。它也被称为整数数组。

As per the problem statement we have to create an Integer array and display the array elements where all the array elements are the product of other elements of the array.

In this article, we will see how to replace array elements by the product of other array elements by using Java programming language.

To show you some instances −

Instance-1

int arr[] = { 2, 3, 1, 4, 6 }
At Index-0 value will be = 3 * 1 * 4 * 6 = 72
At Index-1 value will be = 2 * 1 * 4 * 6 = 48
At Index-2 value will be = 2 * 3 * 4 * 6 = 144
At Index-3 value will be = 2 * 3 * 1 * 6 = 36
At Index-4 value will be = 2 * 3 * 1 * 4 = 24
So, the updated array elements are {72, 48, 144, 36, 24}
登录后复制

Instance-2

int arr[] = { 1, 3, 2, 4 }
At Index-0 value will be = 3 * 2 * 4 = 24
At Index-1 value will be = 1 * 2 * 4 = 8
At Index-2 value will be = 1 * 3 * 4 = 12
At Index-3 value will be = 1 * 3 * 2 = 6
So, the updated array elements are {24, 8, 12, 6}
登录后复制

Algorithm

Algorithm-1

Step-1 − Declare and initialize an integer array.

第二步 - 找到所有数组元素的乘积。

Step-3 − Divide the product value with the value of the respective index and replace the result. Continue this step till you reach the last array element.

步骤-4 − 打印更新后的数组。

Algorithm-2

Step-1 − Declare and initialize an integer array.

Step-2 − Find the product of left sub array elements. The elements before the respective element to be replaced.

步骤-3 − 找到右子数组元素的乘积。在要替换的元素之后的元素。

步骤-4 − 找到左子数组和右子数组的乘积值的和,并替换为结果值。

Step-5 − Continue Step-2, 3 and 4 till you reach the last array element.

Step-6 − 打印更新后的数组。

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it

array.length
登录后复制

where, ‘array’ refers to the array reference.

To get the String representation of the respective array we can use the toString() method of Arrays class in Java.

Arrays.toString(A)
登录后复制

多种方法−

We have provided the solution in different approaches.

  • 通过将各自的指数元素除以总产品值

  • 通过找到左子数组和右子数组的产品值

让我们逐个查看程序及其输出。

Approach-1 − By Dividing the respective Index Element with the Total Product Value

在这种方法中,我们将通过将总产品值除以要替换的相应元素来替换数组元素。

Example

import java.util.Arrays;
public class Main {
   public static void main(String args[]) {
      //Declare and initialize the array elements
      int arr[] = { 2, 3, 1, 4, 6 };
      // Print the array elements
      System.out.println("Array elements are: "+Arrays.toString(arr));
      //Declare an int variable 'product' to hold the product value
      int product = 1;
      //iterate the array elements and find the product of all elements
      for(int i=0;i<arr.length;i++) {
         product*=arr[i];
      }
      //Divide the total product value with the array element to be replaced
      for(int i=0;i<arr.length;i++) {
         arr[i] = product/arr[i];
      }
      //print the new array
      System.out.println("Updated array: "+Arrays.toString(arr));
   }
}
登录后复制

Output

Array elements are: [2, 3, 1, 4, 6]
Updated array: [72, 48, 144, 36, 24]
登录后复制

Approach-2 − By Finding the Product Values of Left and Right Sub Array

In this approach, we will replace the array element finding the sum of product of left subarray elements and product of right sub array elements.

Example

import java.util.Arrays;
public class Main{
   public static void main(String args[]) {
      int arr[] = {4,2,3,1};
      int size=arr.length;
      int temp[]= new int[size];
      int sum=1;
      System.out.println("Array elements are: "+Arrays.toString(arr));
      for(int index=0; index<arr.length; index++) {
         int leftProduct=1;
         int rightProduct=1;
         int i=0;
         if(i<index) {
            for(i=0;i<index;i++) {
               leftProduct*=arr[i];
            } 
         }
         for(i=index+1;i<arr.length;i++){
            rightProduct*=arr[i];
         }
         sum=leftProduct*rightProduct;
         temp[index]=sum;
      }
      arr=temp;
      System.out.println("Updated array: "+Arrays.toString(arr));
   }
}
登录后复制

Output

Array elements are: [4, 2, 3, 1]
Updated array: [6, 12, 8, 24]
登录后复制

在本文中,我们探讨了如何使用不同的方法在Java中将数组元素替换为其他数组元素的乘积。

以上是JAVA程序将整数数组的元素替换为其他元素的乘积的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板