I need to write a recursive function that returns the largest and smallest elements in the ArrayList and the corresponding indices. To return these four items, I need to return an object that is an instance of an inner class called MinMaxObject that has four defined private variables: max, min, maxPos, minPos of types double, double, int and int.
I've gotten to here on my own and I need to start the recursion but I have no idea how to get going. If someone could point me in the right direction I should be able to pick it up.
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;
}
It sounds like recursion is a part of the assignment.
The simplest way is to iterate through the arraylist, but since you need recursion it's a little more complex. Since this is homework, and because I'm lazy, I'm not going to give you compilable Java code. This is a close approximation. I'm also not going to give you the whole method, cause you should figure the rest out from here.
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);
}
Thanks @ScubaSteve! ...I outline little more/detailed:
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);
}
The initial/final/outer invocation would be:
// 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);
A recursion alternative would be: To recourse not only the "head" of the list ( idx : 0 ... size - 1
), but also the "tail" like:
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);
}
the "alternative" halves the count of recursions (but doubles the "inline complexity").
Another "divide & conquer" approach:
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;
}
}
...with (same) O(n)
complexity.
Something like this:
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);
}
Just make sure you initialize your MinMaxObject
with appropriate values (probably should make those part of a default constructor), something like: MinMaxObject minMax = new MinMaxObject(Integer.MAX_VALUE, Integer.MIN_VALUE, -1, -1);
Hope I got the parameter order right.
A search on the terms "recursion" and "accumulator" would probably shed some light on this for you.
Recursion is unnecessary. Using the ArrayList List
, here is a sample.
Collections.sort(List)
return List.get(0); // Least number
return List.get(List.size()-1); // Max number
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)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.