简体   繁体   English

(Java)在传递到 function 后,将通用 T 数组索引作为 int 返回?

[英](Java) Return generic T array index as int after passed into an int function?

I had this 4-part Pset for a class (submission deadline already passed), but I've been dying on problem 1 and the next set Pset is going to be open later this week.我有一个 class 的 4 部分 Pset(提交截止日期已经过去),但我一直在解决问题 1,下一组 Pset 将在本周晚些时候开放。 I've read the HW chapters, gone to office hours (they can help the concept with but can't help with implementing code) and rewatched lectures, but I can't find how I'm supposed to do this.我已经阅读了 HW 章节,去上班时间(他们可以帮助概念但不能帮助实现代码)并重新观看讲座,但我找不到我应该如何做到这一点。

This is the part that I can't change (import java.util.* is included above it):这是我无法更改的部分(上面包含导入 java.util.*):

  public static <T extends Comparable<T>> int findMax(T[] arr)

So it's an int function that passes a type T array and is supposed to return an int index.所以它是一个 int function 传递类型 T 数组并且应该返回一个 int 索引。 I tried a number of ways to try iterating using for & while loops, but the problem is I can't figure out how to do it.我尝试了多种方法来尝试使用 for & while 循环进行迭代,但问题是我不知道该怎么做。

This code below is modified from programmer/blogger Deepesh Darshan's post on finding max values using generics , as it was really similar to one of mine that used a for loop.下面的代码是根据程序员/博主 Deepesh Darshan 关于使用 generics 查找最大值的帖子修改的,因为它与我使用 for 循环的代码非常相似。 the "return -1;" “返回-1;” is in case no code was added.以防万一没有添加代码。

          int maxPoint = arr[0];
      for(T arrScan : arr){
          if(arrScan.compareTo(maxPoint) > 0 ) {
            maxPoint = arrScan;
          }
              return maxPoint; 
      }
     return -1;
  }
    

One issue is his solution uses T the whole way through and mine can't.一个问题是他的解决方案一直使用 T 而我的不能。

public static <T extends Comparable<T>> T max(T... elements)

My code above gives the following errors:我上面的代码给出了以下错误:

GenericMethods.java:16: error: incompatible types: T cannot be converted to int
        int maxPoint = arr[0];
                          ^
  where T is a type-variable:
    T extends Comparable<T> declared in method <T>findMax(T[])
GenericMethods.java:18: error: incompatible types: int cannot be converted to T
          if(arrScan.compareTo(maxPoint) > 0 ) {
                               ^
  where T is a type-variable:
    T extends Comparable<T> declared in method <T>findMax(T[])
GenericMethods.java:19: error: incompatible types: T cannot be converted to int
            maxPoint = arrScan;
                       ^
  where T is a type-variable:
    T extends Comparable<T> declared in method <T>findMax(T[])
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
3 errors

I know it's because I'd need to make maxPoint "T", not "int", but that just kicks the conversion error down to the return value.我知道这是因为我需要将 maxPoint 设置为“T”,而不是“int”,但这只会将转换错误踢到返回值。

Is there some reading or advice someone could point me to on how to find the max index with the given restrictions without causing conversion errors?是否有一些阅读或建议有人可以指出如何找到具有给定限制的最大索引而不会导致转换错误? I can't seem to find any on GFG, W3 or any of the places suggested to me.我似乎无法在 GFG、W3 或向我建议的任何地方找到任何内容。

You need to track the index instead of the element:您需要跟踪索引而不是元素:

int max = 0;
for (int i = 1; i < arr.length; i++) {
    if (arr[i].compareTo(arr[max]) > 0) {
        max = i;
    }
}
return max;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM