Java 返回一个min&;使用递归的最大值及其在arraylist中的位置

Java 返回一个min&;使用递归的最大值及其在arraylist中的位置,java,recursion,arraylist,Java,Recursion,Arraylist,我需要编写一个递归函数,返回ArrayList中最大和最小的元素以及相应的索引。要返回这四个项,我需要返回一个对象,该对象是名为MinMaxObject的内部类的实例,该类有四个已定义的私有变量:max、min、maxPos、minPos,类型为double、double、int和int 我独自来到这里,我需要开始递归,但我不知道如何开始。如果有人能给我指出正确的方向,我应该能把它捡起来 public static void main(String args[]) { Scanner

我需要编写一个递归函数,返回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;
}
publicstaticvoidmain(字符串参数[]){
扫描仪控制台=新扫描仪(System.in);
System.out.print(“请输入文件名”);
字符串文件名=console.next();
试一试{
File fileRef=新文件(文件名);
扫描器令牌=新扫描器(fileRef);
double input=tokens.nextDouble();
ArrayList=新建ArrayList();
while(tokens.hasNextLine()){
列表。添加(输入);
}
System.out.println(“For循环”);
对于(int counter=0;counter
递归是不必要的。使用
数组列表
,下面是一个示例

Collections.sort(List)
return List.get(0); // Least number
return List.get(List.size()-1); // Max number

听起来递归是赋值的一部分

最简单的方法是遍历arraylist,但因为需要递归,所以要复杂一点。因为这是家庭作业,而且因为我很懒,所以我不会给你可编译的Java代码。这是一个近似值。我也不会给你整个方法,因为你应该从这里了解其余部分

private int min = 0;
private int max = 0;
findMinMax(int index, List<int> list)
{
    //in recursion always start your method with a base-case
    if(index >= list.Count)
        return ;

    //find min and max here

    //this is called tail recursion, it's basically the same as a loop
    findMinMax(index + 1, list);
}
private int min=0;
私有int max=0;
findMinMax(整数索引,列表)
{
//在递归中,始终以基本情况开始方法
如果(索引>=list.Count)
返回;
//在这里找到最小值和最大值
//这叫做尾部递归,它基本上与循环相同
findMinMax(索引+1,列表);
}
public void getMinMax(MinMaxObject MinMaxObject,List List,int currentIndex)
{
if(list.get(currentIndex)minMaxObject.getMax())
setMax(list.get(currentIndex));
minMaxObject.setMaxPos(currentIndex);
if(currentIndex
类似这样的内容:

MinMaxObject findMaxMin(final ArrayList<Double> nums, MinMaxObject minMax, int pos){

        //Base Case
        if(pos >= nums.size()){
            return minMax;
        }

        //Check to see if the current element is bigger than max or smaller than min
        Double value = nums.get(pos); 
        if(value > minMax.getMax()){
            //adjust minMax

        }
        if(value < minMax.getMin()){
            //adjust minMax
        }

        //recursion
        return findMaxMin(nums, minMax, pos+1);


    }
MinMaxObject findMaxMin(最终数组列表nums,MinMaxObject minMax,int pos){
//基本情况
如果(位置>=nums.size()){
返回最小最大值;
}
//检查当前元素是否大于最大值或小于最小值
双值=nums.get(pos);
如果(值>minMax.getMax()){
//调整最小最大值
}
if(值
只需确保使用适当的值初始化
MinMaxObject
(可能应该使这些值成为默认构造函数的一部分),例如:
MinMaxObject minMax=new MinMaxObject(Integer.MAX_值,Integer.MIN_值,-1,-1);

希望我得到了正确的参数顺序


搜索“递归”和“累加器”这两个术语可能会让您对此有所了解。

谢谢@ScubaSteve!…我将略作详细介绍:

void findMinMax(int idx, List items, MinMaxObject result) {
    // this is good:
    if (idx >= items.size()) {
        return;
    }
    // check whether list[idx] is min or max: compare with & store to 'result'

    // recursion:
    findMinMax(idx + 1, items, result);
}
初始/最终/外部调用将是:

// initialize with high min, low max and "out of range" indices
// ...alternatively in default constructor/field declaration.
MinMaxObject result = new MinMaxObject(Integer.MAX_VALUE, Integer.MIN_VALUE, -1, -1);
// start recursion at index 0:
findMinMax(0, items, result);

递归的替代方法是:不仅使用列表的“头”(
idx:0…size-1
),还可以使用类似以下内容的“尾”:

“可选”将递归计数减半(但将“内联复杂性”增加一倍)


另一种“分而治之”的方法:

void findMinMax(int left, int right, items, result) {
    if(left > right) {
        return;// alternatively do nothing
    } else if (left == right) {
       //check items[left] ...
       // return;
    } else { // left < right
        int mid = (right - left) / 2;
        findMinMax(left, mid, items, result);
        findMinMax(mid + 1, right, items, result);
        // return;
    }
}    
void findMinMax(int left、int right、items、result){
如果(左>右){
return;//或者不执行任何操作
}else if(左==右){
//检查项目[左]。。。
//返回;
}else{//left
…具有(相同)
O(n)
复杂性

void findMinMax(int head, int tail, List items, MinMaxObject result) {
    if(head > tail) {
        // done!
        return;
    }
    // check items[head] and items[tail]... store to result
    // iterate:
    return findMinMax(head + 1, tail - 1, items, result);
}
void findMinMax(int left, int right, items, result) {
    if(left > right) {
        return;// alternatively do nothing
    } else if (left == right) {
       //check items[left] ...
       // return;
    } else { // left < right
        int mid = (right - left) / 2;
        findMinMax(left, mid, items, result);
        findMinMax(mid + 1, right, items, result);
        // return;
    }
}