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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

在本文中,我们旨在探讨关于多种编程语言中数组的最大公约数(GCD)的一个引人入胜的问题,重点放在C++上。我们将展示一种算法方法,利用成对元素交换以及它们的乘积数量来验证是否可以将GCD提高到1以上。此外,我们还将提供解决这个问题的其他方法,每种方法都有其语法定义。除了这些解决方案,我们还将呈现两个完整的可执行代码,其中包含了这些方法。语法为了确保对后续代码示例有清晰的理解,我们必须在此之前评估和理解所使用的语法。#include<iostream>#include<vecto

给定一个数字N,我们必须将这个数字与其最大的数字相乘。如果没有数字,则打印-1。就像我们用“153”初始化N,并且该数字中的最大的数字是5,否则结果将是153与5的乘积,即153 * 5 = 765,并且如果该数字有

简介在数学中,我们看到了需要将数字乘以一定幂的问题。我们将通过一个例子来理解这个问题。想象一下,我们有一个数字列表,我们想要将相同的元素与其自身相乘一定次数。这就是“在列表中查找i^k的乘积”程序有用的地方。在数学领域,我们将这个过程称为求幂。它有助于解决大多数数学计算和问题。我们将用python编写代码。使用python更容易解决此类问题。Python与所有编程语言不同,它包含特殊的工具,例如使编程世界变得更轻松且节省时间的库。Python中的库包含有助于快速解决数学问题的函数。意味着无需从头

如何使用Go语言中的排序函数对整数数组进行升序排序?Go语言是一门简洁高效的编程语言,提供了一系列强大的标准库函数来满足不同的编程需求。其中,排序函数是Go语言中常用的功能之一。本文将介绍如何使用Go语言中的排序函数对整数数组进行升序排序,并附带相关的代码示例。在Go语言中,我们可以使用sort包中的函数来进行排序操作。sort包提供了多种排序算法,包括快速

整数数组是指所有元素都是整数类型的数组。它也被称为整数数组。AspertheproblemstatementwehavetocreateanIntegerarrayanddisplaythearrayelementswhereallthearrayelementsaretheproductofotherelementsofthearray.Inthisarticle,wewillseehowtoreplacearrayelementsbytheproductofotherarrayelement

Introduction在Python中有不同类型的数据结构。元组是一种数据结构,它是一个有序的元素集合。元组也被称为不可变的。不可变基本上意味着一旦创建,元组就无法修改。在下面的文章中,我们将了解在Python中查找选择元组键的乘积的程序。在需要对元组中特定元素进行乘法运算的问题中,这个程序非常有用。UnderstandingtheProblemTupleisinitializedinasimilarwayweintializethelistinpython.Tuplestoresaseque

递归是一种从同一个函数本身调用函数的技术。必须有一些基本或终止条件来结束递归调用。递归过程对于用更少的代码执行复杂的迭代求解非常有帮助,并且通过子操作查找更容易的求解方法。在本文中,我们将讨论在C++中执行两个数字之间的乘积(乘法)的递归方法。首先我们了解基本原理、递归函数调用语法、算法和源码。使用递归的乘法在高级语言中,有乘法运算符可以直接执行乘法。然而我们知道,乘法实际上是重复的加法。所以A*B的结果就是A、B的重复相加次数,或者可以说B、A的重复相加次数。每当有重复时,我们可以使用递归来做

数据结构操作现在已成为现代编程和计算中成功解决方案开发的一个重要方面。这是由于随着时间的推移,这些结构所呈现的复杂性不断增加。一个例子是执行交换操作以最小化包含在两个数组中的最大数的总和,从而降低它们的整体值。在这篇文章中,我们讨论了两种使用C++完成这些任务的方法,同时根据不同观点承认了这两种方法的优点和缺点。语法为了有效地理解C++编程语言中的方法和代码,我们需要对基本语法有扎实的理解。这意味着要仔细研究与我们手头的主题相关的组件。Arrays:intarrayName[size];Sort
