【发布时间】:2016-02-07 09:03:53
【问题描述】:
所以对于一个编程类,我们需要递归地找到一个向量的最大值和最小值。我知道如何使用迭代来做到这一点,但无法弄清楚我需要做什么才能使用此分配所需的递归。如果我可以获取索引,我将能够仅使用 element at 获取最大值和最小值,但到目前为止我尝试的所有内容都会让我产生大量错误。这是我目前拥有的所有代码。
import java.util.*;
import java.io.*;
public class Lab4<E> {
Vector <Double> data;
public void readData() throws FileNotFoundException
{
{
Scanner console = new Scanner (System.in);
System.out.println("Enter the data file name: ");
String inFile = console.next();
File fileRef = new File(inFile);
Scanner tokens = new Scanner(fileRef);
data = new Vector<Double>();
while(tokens.hasNext())
{
Double value = tokens.nextDouble();
data.add(value);
}
System.out.print("The values in the file are: "+data.toString()+"\n");
System.out.print("The number of values in the file is: "+(data.size())+"\n");
tokens.close();
console.close();
}
}
public static void main(String[] args)
throws IOException
{
Lab4 fileTest = new Lab4();
fileTest.readData();
System.out.println(obj);
}
class MinMaxObject
{
private double max, min;
private int maxPos, minPos;
public MinMaxObject()
{
this.max =Double.MIN_VALUE;
this.maxPos = 0;
this.min=Double.MAX_VALUE;
this.minPos = 0;
}
public MinMaxObject(double ma, int maP, double mi, int miP)
{
this.max = ma;
this.maxPos = maP;
this.min = mi;
this.minPos = miP;
}
public void setMax(double newMax)
{
max = newMax;
}
public double getMax() {
return max;
}
public void setMin(double newMin)
{
min = newMin;
}
public double getMin() {
return min;
}
public void setMaxPos(int newMaxPos)
{
maxPos = newMaxPos;
}
public int getMaxPos() {
return maxPos;
}
public void setMinPos(int newMinPos)
{
minPos = newMinPos;
}
public int getMinPos() {
return minPos;
}
}
}
【问题讨论】:
标签: java recursion vector inner-classes