Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 查找最大元素的索引_Java_Arrays_Recursion_Max - Fatal编程技术网

Java 查找最大元素的索引

Java 查找最大元素的索引,java,arrays,recursion,max,Java,Arrays,Recursion,Max,我很难得到找到最大元素的索引。我知道数组中的元素可以被X[r]访问,其中r是索引,这就是我在这里所做的,但我似乎无法得到它,只能得到索引 代码: public class Max { public static void main(String[] args) { int[] B = {-1, 2, 6, 3, 9, 2, -3, -2, 11, 5, 7}; System.out.println("max = " + maxArrayIndex(B, 0, B.length-

我很难得到找到最大元素的索引。我知道数组中的元素可以被X[r]访问,其中r是索引,这就是我在这里所做的,但我似乎无法得到它,只能得到索引

代码:

public class Max {

public static void main(String[] args) {
    int[] B = {-1, 2, 6, 3, 9, 2, -3, -2, 11, 5, 7};
    System.out.println("max = " + maxArrayIndex(B, 0, B.length-1));
}

static int maxArrayIndex(int[] X, int p, int r) {
    int q = 0;
    if(p < r) {
       q = (p + r)/2;
       int maxLeft = maxArrayIndex(X, p, q);
       int maxRight = maxArrayIndex(X, q+1, r);
       int maxFinal = max(maxLeft, maxRight);
       return maxFinal;
      }
      return X[r];
   }

   static int max(int p , int r) {
       int maxIndex = 0;
       if(p > r) {
          maxIndex = p;
       } else {
           maxIndex = r;
       }
       return maxIndex;
     }
}
公共类最大值{
公共静态void main(字符串[]args){
int[]B={-1,2,6,3,9,2,-3,-2,11,5,7};
System.out.println(“max=“+maxarayindex(B,0,B.length-1));
}
静态int-maxarayindex(int[]X,int-p,int-r){
int q=0;
if(pr){
最大指数=p;
}否则{
maxIndex=r;
}
返回最大索引;
}
}

似乎您从未检查数组中的元素,而是在最后返回索引值

 static int max(int[] X , int maxIndex) {
    for (int i = 1; i <  X.length; i++){
       int currentNumber=  X[i];
       if ((currentNumber>  X[maxIndex])){
       maxIndex = i;
      }
    } 
    return maxIndex; 
} 
static int max(int[]X,int maxIndex){
对于(int i=1;iX[maxIndex])){
maxIndex=i;
}
} 
返回最大索引;
} 
检查这个问题,它看起来很像你想做的:

似乎您从未检查数组中的元素,而是在最后返回索引值

 static int max(int[] X , int maxIndex) {
    for (int i = 1; i <  X.length; i++){
       int currentNumber=  X[i];
       if ((currentNumber>  X[maxIndex])){
       maxIndex = i;
      }
    } 
    return maxIndex; 
} 
static int max(int[]X,int maxIndex){
对于(int i=1;iX[maxIndex])){
maxIndex=i;
}
} 
返回最大索引;
} 
检查这个问题,它看起来很像你想做的:

公共类最大值{
公共静态void main(字符串[]args){
int[]B={-1,2,6,3,9,2,-3,-2,11,5,7};
System.out.println(“max=“+maxarayindex(B,0,B.length-1));
}
静态int-maxarayindex(int[]X,int-p,int-r){
int q=0;
if(pX[r]){
最大指数=p;
}否则{
maxIndex=r;
}
返回最大索引;
}
}
建议的备选方案:

static int maxArrayIndex(int[] X, int p, int r) {
    int currentMaxIndex = 0;
    for (int i = 0; i < X.length; i++) {
        if(X[i] > X[currentMaxIndex]){
            currentMaxIndex = i;
        }
    }
    return currentMaxIndex;
}
静态int-maxarayindex(int[]X,int-p,int-r){
int currentMaxIndex=0;
对于(int i=0;iX[currentMaxIndex]){
currentMaxIndex=i;
}
}
返回currentMaxIndex;
}
公共类最大值{
公共静态void main(字符串[]args){
int[]B={-1,2,6,3,9,2,-3,-2,11,5,7};
System.out.println(“max=“+maxarayindex(B,0,B.length-1));
}
静态int-maxarayindex(int[]X,int-p,int-r){
int q=0;
if(pX[r]){
最大指数=p;
}否则{
maxIndex=r;
}
返回最大索引;
}
}
建议的备选方案:

static int maxArrayIndex(int[] X, int p, int r) {
    int currentMaxIndex = 0;
    for (int i = 0; i < X.length; i++) {
        if(X[i] > X[currentMaxIndex]){
            currentMaxIndex = i;
        }
    }
    return currentMaxIndex;
}
静态int-maxarayindex(int[]X,int-p,int-r){
int currentMaxIndex=0;
对于(int i=0;iX[currentMaxIndex]){
currentMaxIndex=i;
}
}
返回currentMaxIndex;
}

像这样修改
maxarayindex
&max方法,您必须在
块之外再次调用
max
,如果
块,其余的代码都可以

代码中的问题:您必须将数组
X
传递给
max
方法才能获得较大元素的索引,现在您只需找到较大的索引

static int maxArrayIndex(int[] X, int p, int r) {
int q = 0;
if(p < r) {
   q = (p + r)/2;
   int maxLeft = maxArrayIndex(X, p, q);
   int maxRight = maxArrayIndex(X, q+1, r);
   return max(X,maxLeft, maxRight);
  }
  return max(X,p,r);
}


static int max(int X[],int p , int r) {
   int maxIndex = 0;
   if(X[p] > X[r]) {
      maxIndex = p;
   } 
   else {
      maxIndex = r;
   }
   return maxIndex;
 }
静态int-maxarayindex(int[]X,int-p,int-r){
int q=0;
if(pX[r]){
最大指数=p;
} 
否则{
maxIndex=r;
}
返回最大索引;
}

像这样修改
maxarayindex
&max方法,您必须在
块之外再次调用
max
,如果
块,其余的代码都可以

代码中的问题:您必须将数组
X
传递给
max
方法才能获得较大元素的索引,现在您只需找到较大的索引

static int maxArrayIndex(int[] X, int p, int r) {
int q = 0;
if(p < r) {
   q = (p + r)/2;
   int maxLeft = maxArrayIndex(X, p, q);
   int maxRight = maxArrayIndex(X, q+1, r);
   return max(X,maxLeft, maxRight);
  }
  return max(X,p,r);
}


static int max(int X[],int p , int r) {
   int maxIndex = 0;
   if(X[p] > X[r]) {
      maxIndex = p;
   } 
   else {
      maxIndex = r;
   }
   return maxIndex;
 }
静态int-maxarayindex(int[]X,int-p,int-r){
int q=0;
if(pX[r]){
最大指数=p;
} 
否则{
maxIndex=r;
}
返回最大索引;
}

使用调试器,这就是调试器的用途。如@dambros