在数组中查找最大整数,在Java中使用指定的开始和结束索引,**递归**
对于我正在研究的东西,我需要一个整数数组,并从用户那里获得两个索引。一个是数组中的起点,另一个是终点。利用这两点,我必须递归地找到数组中的最大值。我目前拥有的代码很有效,但我觉得有些东西可以让它更流畅,甚至我可以做一些不同的事情来摆脱一些无用的代码。我将在下面发布我的代码在数组中查找最大整数,在Java中使用指定的开始和结束索引,**递归**,java,recursion,integer,Java,Recursion,Integer,对于我正在研究的东西,我需要一个整数数组,并从用户那里获得两个索引。一个是数组中的起点,另一个是终点。利用这两点,我必须递归地找到数组中的最大值。我目前拥有的代码很有效,但我觉得有些东西可以让它更流畅,甚至我可以做一些不同的事情来摆脱一些无用的代码。我将在下面发布我的代码 import java.util.Scanner; public class RecursiveProblem { public static void main(String args[]) {
import java.util.Scanner;
public class RecursiveProblem {
public static void main(String args[]) {
int start, end, size, max;
Scanner scan = new Scanner(System.in);
System.out.println("How many numbers would you like to enter in total?");
size = scan.nextInt();
int[] myArray = new int[size];
System.out.println("Please enter the specified starting index.");
start = scan.nextInt() - 1;
System.out.println("Please enter the specified ending index.");
end = scan.nextInt() - 1;
System.out.println("Please enter " + size + " integers seperated by a space, or press "
+ "enter after each number: ");
for(int i = 0; i < myArray.length; i++) {
myArray[i] = scan.nextInt();
}
scan.close();
max = myArray[start];
max = findMax(myArray, start, end, max);
System.out.println("The max of your array between the indices of " + (start + 1) +
"-" + (end + 1) + " is..." + max);
}
public static int findMax(int[] myArray, int start, int end, int max) {
if(start < end) {
if(myArray[start + 1] > max) {
start++;
max = myArray[start];
return findMax(myArray, start, end, max);
}
else {
start++;
return findMax(myArray, start, end, max);
}
}
return max;
}
}
import java.util.Scanner;
公共类递归问题{
公共静态void main(字符串参数[]){
int开始、结束、大小、最大值;
扫描仪扫描=新扫描仪(System.in);
System.out.println(“您希望总共输入多少个数字?”);
size=scan.nextInt();
int[]myArray=新的int[size];
System.out.println(“请输入指定的起始索引”);
start=scan.nextInt()-1;
System.out.println(“请输入指定的结束索引”);
end=scan.nextInt()-1;
System.out.println(“请输入“+size+”以空格分隔的整数,或按”
+“在每个数字后面输入:”;
for(int i=0;imax){
启动++;
max=myArray[start];
返回findMax(myArray、start、end、max);
}
否则{
启动++;
返回findMax(myArray、start、end、max);
}
}
返回最大值;
}
}
有一件事我主要感到困惑,但这不是我唯一的问题,那就是每当我选择将start++命令放在findMax(myArray,start++,end,max)的函数调用中时,您的代码总体上是可以的,但是如果您想要简洁的东西,可以使用下面的代码:
//Before calling this particular method, you'll have to set the max to
//Integer.MINIMUM_VALUE so it's guaranteed to be changed later OR call it
//with start + 1 instead of start
public static int findMax(int[] myArray, int start, int end, int max) {
return (start < end) ?
findMax(myArray,
start + 1,
end,
(myArray[start] > max) ? myArray[start] : max))
: max;
}
//在调用此特定方法之前,必须将max设置为
//Integer.MINIMUM_值,保证以后更改或调用它
//使用start+1而不是start
公共静态int findMax(int[]myArray,int start,int end,int max){
返回(开始<结束)?
findMax(myArray,
开始+1,
终止
(myArray[开始]>最大值)?myArray[开始]:最大值)
:最大值;
}
无限重复的原因是因为后增量操作符的工作方式
当你说
start++
时,它会将start的值推到堆栈上,然后在后台递增。因此,作为一个独立的语句是可以的,但是当您将其编写为findMax(myArray,start++,end,max)
时,它实际上与调用findMax(myArray,start,end,max)
是一样的,因为参数start实际上是不变的。您的代码总体上是可以的,但是如果您想要简洁的东西,可以使用下面的代码:
//Before calling this particular method, you'll have to set the max to
//Integer.MINIMUM_VALUE so it's guaranteed to be changed later OR call it
//with start + 1 instead of start
public static int findMax(int[] myArray, int start, int end, int max) {
return (start < end) ?
findMax(myArray,
start + 1,
end,
(myArray[start] > max) ? myArray[start] : max))
: max;
}
//在调用此特定方法之前,必须将max设置为
//Integer.MINIMUM_值,保证以后更改或调用它
//使用start+1而不是start
公共静态int findMax(int[]myArray,int start,int end,int max){
返回(开始<结束)?
findMax(myArray,
开始+1,
终止
(myArray[开始]>最大值)?myArray[开始]:最大值)
:最大值;
}
无限重复的原因是因为后增量操作符的工作方式
当你说
start++
时,它会将start的值推到堆栈上,然后在后台递增。因此,作为一个独立的语句是可以的,但当您将其编写为findMax(myArray,start++,end,max)
时,它实际上与调用findMax(myArray,start,end,max)
是一样的,因为参数start实际上是不变的。您的思路是正确的,但您的解决方案可以简化
关键观察结果是数组的最大元素为:
// requires start <= end (not checked, left as exercise for reader)
public static int findMax(int[] myArray, int start, int end) {
if (start < end) {
int maxOfRest = findMax(myArray, start+1, end);
return myArray[start] > maxOfRest ? myArray[start] : maxOfRest;
}
else { // start == end
return myArray[start];
}
}
//需要启动maxOfRest吗?myArray[start]:maxOfRest;
}
else{//start==end
返回myArray[start];
}
}
脚注:
?:
运算符正确地称为条件运算符。它有时被称为“三元运算符”,但这意味着“三个操作数的运算符”,不同于各种二元运算符(两个操作数)和一元运算符(一个操作数),因此它的信息量不大。您的思路是正确的,但您的解决方案可以简化
关键观察结果是数组的最大元素为:
// requires start <= end (not checked, left as exercise for reader)
public static int findMax(int[] myArray, int start, int end) {
if (start < end) {
int maxOfRest = findMax(myArray, start+1, end);
return myArray[start] > maxOfRest ? myArray[start] : maxOfRest;
}
else { // start == end
return myArray[start];
}
}
//需要启动maxOfRest吗?myArray[start]:maxOfRest;
}
else{//start==end
返回myArray[start];
}
}
脚注:
?:
运算符正确地称为条件运算符。它有时被称为“三元运算符”,但与各种二元运算符(两个操作数)和一元运算符不同,这意味着“三个操作数的运算符”