我需要编写一个递归函数,返回ArrayList中最大和最小的元素以及相应的索引。要返回这四项,我需要返回一个对象,该对象是名为MinMaxObject的内部类的实例,它有四个已定义的私有变量:max、min、maxPos、minPos类型为double、double、int和int。
我自己到了这里,我需要开始递归,但我不知道如何开始。如果有人能给我指明正确的方向,我就能把它捡起来。
public static void main(String args[]) {
Scanner console = new Scanner(System.in);
System.out.print("Please enter a file name");
String fileName = console.next();
try {
File fileRef = new File(fileName);
Scanner tokens = new Scanner(fileRef);
double input = tokens.nextDouble();
ArrayList<Double> list = new ArrayList<Double>();
while (tokens.hasNextLine()) {
list.add(input);
}
System.out.println("For Loop");
for (int counter = 0; counter < list.size(); counter++) {
System.out.println(list.get(counter));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 2 functions, one initialize & one to call the original recursion
//can start at beggining and recurse to the end or vice versa
//updates the min and max as it goes
//update minPos and maxPos as well
}
public class MinMaxObject {
private double min;
private double max;
private int minPos;
private int maxPos;
public MinMaxObject(double newMin, double newMax, int newMinPos, int newMaxPos){
min = newMin;
max = newMax;
minPos = newMinPos;
maxPos = newMaxPos;
}
public double getMin(){
return min;
}
public void setMin(double newMin){
min = newMin;
}
public double getMax(){
return max;
}
public void setMax(double newMax){
max = newMax;
}
public int getMinPos(){
return minPos;
}
public void setMinPos(int newMinPos){
minPos = newMinPos;
}
public int getMaxPos(){
return maxPos;
}
public void setMaxPos(int newMaxPos){
maxPos = newMaxPos;
}
public void getMinMax(MinMaxObject minMaxObject, List list, int currentIndex)
{
if(list.get(currentIndex) < minMaxObject.getMin())
minMaxObject.setMin(list.get(currentIndex) );
minMaxObject.setMinPos(currentIndex) ;
else if(list.get(currentIndex) > minMaxObject.getMax())
minMaxObject.setMax(list.get(currentIndex));
minMaxObject.setMaxPos(currentIndex) ;
if(currentIndex < list.size-1) getMinMax(minMaxObject, list, ++currentIndex)
}
那么我如何使用这个pair类和我的方法来找到最小值和最大值。
我们想写一个函数,它将二叉树的根作为输入,并使用类PairAns返回该树的最大值和最小值。 我在这个问题的基础案例中遇到了一些问题 我希望答案是正确的,但在所有测试用例中都出现了运行时错误。
本文向大家介绍python寻找list中最大值、最小值并返回其所在位置的方法,包括了python寻找list中最大值、最小值并返回其所在位置的方法的使用技巧和注意事项,需要的朋友参考一下 实例如下所示: 以上这篇python寻找list中最大值、最小值并返回其所在位置的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教程。
本文向大家介绍返回JavaScript中数组的最小值和最大值的函数,包括了返回JavaScript中数组的最小值和最大值的函数的使用技巧和注意事项,需要的朋友参考一下 问题 我们需要编写一个接受一个数组并返回另一个数组的JavaScript函数,该数组的第一个元素应该是输入数组的最小元素,第二个应该是输入数组的最大元素。 示例 以下是代码- 输出结果
本文向大家介绍使用递归实现指定最小值和最大值之间的所有整数求和相关面试题,主要包含被问及使用递归实现指定最小值和最大值之间的所有整数求和时的应答技巧和注意事项,需要的朋友参考一下 循环 function sumNumber(min, max) { let result = 0 for (let i = min+1; i<max;i++){ result += i } return result }