用Java找出一个整数数组中最大的值,并返回最大值的数组下标,尤其是有两个或是三个最大值时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用Java找出一个整数数组中最大的值,并返回最大值的数组下标,尤其是有两个或是三个最大值时相关的知识,希望对你有一定的参考价值。

用Java找出一个整数数组中最大的值,并返回最大值的数组下标,尤其是有两个或是三个最大值时

参考技术A 用ArrayList做的,可以满足要求

import java.util.*;

public class wenwen
public static void main(String[] args)

ArrayList al=new ArrayList();
int[] a = new int[]11,2,9,15,13,18,14,7,12,18,9,3,1;
int max=0;
for(int i=1;i<a.length;i++)
if(a[max]<=a[i])
max=i;
if(!al.isEmpty()&&a[(Integer)al.get(0)]==a[max])
al.add(max);
else

al.clear();
al.add(max);


System.out.print("最大数是:"+a[(Integer)al.get(0)]+" 下标是:");
for(int i=0;i<al.size();i++)
System.out.print(al.get(i)+" ");

参考技术B import java.io.*;
class InputData
static public String s;
static public void inputdata()
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
s=br.readLine();
catch(IOException e)

static public int getInt()

inputdata();
return Integer.parseInt(s);


public class find_max
/**
* @param args
*/
private int size1;
private int []array;
public static void main(String[] args)
// TODO Auto-generated method stub
find_max obj=new find_max();

System.out.println("please input the size of the array : ");
obj.size1=InputData.getInt();
obj.array=new int [obj.size1];
for(int i=0;i<obj.size1;++i)

obj.array[i]=(int)(Math.random()*1000);
//obj.array[i]=InputData.getInt();
System.out.println(obj.array[i]);

for(int i=0;i<obj.size1-1;i++)
for(int j=i+i;j<obj.size1;j++)

if(obj.array[i]>obj.array[j])

int temp=obj.array[i];
obj.array[i]=obj.array[j];
obj.array[j]=temp;


for(int i=0;i<obj.size1;++i)

System.out.println(obj.array[i]);


参考技术C package com.sento.dao;
public class BigNumber
public int[] getBigNumber(int[] arrary)
int[] index=new int[arrary.length];
int[] tem=arrary;
int temp=0;
int n=0;//记录最大值的下标
int flag=0;
//升序排列,冒泡排序
for(int i=0;i<arrary.length;i++)
for(int j=arrary.length-1;j>i;j--)
if(arrary[j]<arrary[j-1])
temp=arrary[j];
arrary[j]=arrary[j-1];
arrary[j-1]=temp;



flag=tem[tem.length-1];
for(int count=0;count<tem.length;count++)
if(tem[count]==flag)
index[n]=count;
n++;


return index;

以上是关于用Java找出一个整数数组中最大的值,并返回最大值的数组下标,尤其是有两个或是三个最大值时的主要内容,如果未能解决你的问题,请参考以下文章

2021-10-18:乘积最大子数组。给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。力扣152。

用C#找出数组中的最大值和最小值

JAVA作业,找出一个数组中非零最小正整数

找出一个整数数组的和最大的连续子数组

用java实现返回一个整数数组中最大数组的和

Java中,给一个整数数组,找出其中所有的正整数,并求所有正整数的和