Java:<返回二维数组中最大值及下标>
2017-09-30 00:19
731 查看
设计一个名为Location的类,定位二维数组中的最大值及其位置。这个类包括公共的数据域row、column和maxValue,二维数组中的最大值及其下标用int型的row和column以及double型的maxValue存储。编写下面的方法,返回一个二维数组中最大值的位置。
public static Location locateLargest(double[][] a)
返回值是一个Location的实例。
贴代码
public static Location locateLargest(double[][] a)
返回值是一个Location的实例。
贴代码
import java.util.Scanner; public class Exercise8_13 { public static void main(String[] args) { System.out.println("Enter the number of rows and colums of the array :"); Scanner input = new Scanner(System.in); int row = input.nextInt(); int column = input.nextInt(); System.out.println("Enter the array"); double[][] array = new double[row][column]; for(int i=0 ;i < array.length;i++){ for(int j=0 ;j <array[i].length ;j++ ){ array[i][j] = input.nextDouble(); } } Location location1=locateLargest(array); System.out.println("The location of the largets element is "+location1.maxValue+"at "+"("+location1.row+","+location1.column+")"); } public static Location locateLargest(double[][] a) { Location location = new Location(); int row = 0; int column = 0; double maxValue = 0; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if(a[i][j]>maxValue){ maxValue=a[i][j]; row =i; column=j; } } } location.row = row; location.column = column; location.maxValue = maxValue; return location;//实例 } } class Location{ public int row; public int column; public double maxValue; }
相关文章推荐
- Introduction to Java Programming编程题8.13<返回二维数组中最大值及下标>
- java thrift返回List<String>异常
- JAVA中,List<Interger>集合怎么删除特定值的元素,不是根据下标
- Linq学习__03__数据源是IEnumerable<T> 变量,Concat,匿名类,数组下标,返回字符串,group by,orderby
- Java中求List<double[]>中的最大值和最小值
- Java中使用hql,sql查询返回的list<Object> 转成需要的实体对象--方法讲解!
- 数字问题之最大公约数问题全解法归纳<Java实现>
- scala返回List<Object>类型给java
- jni实现返回值为java类型的String[]和List<E>的函数
- mybatis调用oracle存储过程返回游标 读取到java的List<Map>
- 数组问题之一维最大字段和问题<Java实现>
- java中返回任意类型值( <V> V get(Object obj))
- IDEA编译报无法确定 <T>T 的类型参数 ;对于上限为int,java.lang.Object 的类型变量 T,不存在唯一最大实例,解决方案
- java红包算法·返回List<Double>
- 无法确定 <E>E 的类型参数;对于上限为 int,java.lang.Object 的类型变量 E,不存在唯一最大实例
- 相同的代码在eclipse中编译成功,但是用ant编译失败。无法确定<T>T的类型参数;对于上限为int,java.lang.Object的类型变量T,不存在唯一最大实例
- 扑克牌洗牌问题<用java写出算法:54张扑克,分成上下两等份有规律的洗牌,多少次可以返回初始值>
- java笔记--用ThreadLocal管理线程,Callable<V>接口实现有返回值的线程
- Java 二维数组转换成List<List<String>>
- 分治算法求最大子数组以及其对应的下标<c/c++源代码>