0

0

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

WBOY

WBOY

发布时间:2023-09-06 09:33:06

|

876人浏览过

|

来源于tutorialspoint

转载

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.

立即学习Java免费学习笔记(深入)”;

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

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.

立即学习Java免费学习笔记(深入)”;

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.

Cogram
Cogram

使用AI帮你做会议笔记,跟踪行动项目

下载

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

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

Output

Array elements are: [4, 2, 3, 1]
Updated array: [6, 12, 8, 24]

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

相关文章

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
java
java

Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

802

2023.06.15

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

722

2023.07.05

java自学难吗
java自学难吗

Java自学并不难。Java语言相对于其他一些编程语言而言,有着较为简洁和易读的语法,本专题为大家提供java自学难吗相关的文章,大家可以免费体验。

727

2023.07.31

java配置jdk环境变量
java配置jdk环境变量

Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

395

2023.08.01

java保留两位小数
java保留两位小数

Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

398

2023.08.02

java基本数据类型
java基本数据类型

java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。

445

2023.08.02

java有什么用
java有什么用

java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。

428

2023.08.02

java在线网站
java在线网站

Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。

16861

2023.08.03

桌面文件位置介绍
桌面文件位置介绍

本专题整合了桌面文件相关教程,阅读专题下面的文章了解更多内容。

0

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 6.3万人学习

Django 教程
Django 教程

共28课时 | 2.6万人学习

Excel 教程
Excel 教程

共162课时 | 10.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号