简体   繁体   English

泛型和传递数组作为参数的麻烦

[英]Trouble with Generics and passing arrays as parameters

So I am having trouble with a project I am working on. 因此,我在从事的项目遇到麻烦。 The project sounds very simple, I was able to complete 2/3 of it. 该项目听起来很简单,我能够完成其中的2/3。 I have to add 3 generic methods to my class MyGenerics. 我必须在类MyGenerics中添加3个泛型方法。 The method are: 该方法是:

a. 一种。 mymin, where an array (of indeterminate type) is passed in as a parameter and the minimum element is returned. mymin,其中将数组(不确定类型)作为参数传入,并返回最小元素。

b. max2, which accepts an array (of indeterminate type) is passed in as a parameter and the largest two elements are returned. 接受数组(不确定类型)的max2作为参数传入,并返回最大的两个元素。

c. C。 median, which accepts an array (of indeterminate type) is passed in as a parameter and the median is returned. 接受一个数组(不确定类型)的中位数作为参数传入,并返回中位数。

I was able to complete a and c very easily. 我很容易就能完成a和c。 my issue is with b. 我的问题是与b。 I cannot figure out how to return the max, as well as the second largest value. 我不知道如何返回最大值以及第二个最大值。 My initial idea was to get the initial max value in the array, then remove it and get the max again, so that the "new" max would then be the second highest. 我最初的想法是获取数组中的初始最大值,然后将其删除并再次获得最大值,这样“新”最大值将成为第二高的值。 Here is my code: 这是我的代码:

public class MyGenerics {

public static void main(String[] args) {
    Integer intArray[] = { 13, 25, 46, 65, 12, 23};
    Double doubleArray[] = {1.2, 3.4, 1.1, 0.1, 5.6};
    String stringArray[] = {"H", "E", "L", "L", "O"};

    System.out.println("The smallest number is: " + myMin(doubleArray));
    System.out.println("The median is: " + median(doubleArray));
    System.out.println("The median is: " + median(stringArray));
    System.out.println("The max is: " + max2(intArray));
}

 public static <E extends Comparable<E>> E myMin(E... elements) {

        E min = elements[0];
        for (E element : elements) {
            if (element.compareTo(min) < 0) {
                min = element;
            }
        }
        return min;
    }

 public static <E extends Comparable<E>> E max2(E... elements) {

      E max = elements[0];
        for (E element : elements) {
            if (element.compareTo(max) > 0) {
                max = element;
            }
        }
        return max; <-- so obviously this returns the max value of the elements
                        how can i return the max, as well as the second largest value?
    }



public static <E extends Comparable<E>> E median(E... elements) {
 Arrays.sort(elements);

 E median = elements[elements.length/2];

 return median;


 }
}

Hopefully this makes as much sense as possible. 希望这尽可能地有意义。 again, what can I do to now only get the max value to print, but also the second largest value. 再次,我现在该怎么做才能只获得要打印的最大值,但要获得第二个最大值。 Thank you. 谢谢。

Arrays are low-level constructs; 数组是低级构造。 generally, use collection APIs instead. 通常,请改用集合API。

I'm not sure if your confusion is about making a 2-length array, or if it's about finding the top 2 elements. 我不确定您的困惑是要创建2个长度的数组,还是要找到前2个元素。

If it's the former: 如果是前者:

Making new arrays of generic types is normally impossible (because of erasure), but in this case you can crib off of your existing array: 通常无法创建新的泛型类型数组(由于要擦除),但是在这种情况下,您可以从现有数组中删除:

E[] out = (E[]) java.lang.reflect.Array.newInstance(elements.getClass().getComponentType(), 2);

yup, ugly, weird, convoluted, and will generate some generics warnings you'll have to ignore. 是的,丑陋,怪异,令人费解,并且会生成一些泛型警告,您必须忽略它们。

As far as the part about tracking the 2 biggest goes, You can use the same trick as you've used so far, just, with more comparisons (compare any number to both the max and the second-to-max). 至于跟踪最大的2个部分,您可以使用到目前为止所使用的技巧,只是进行更多的比较(将任何数字与最大值和第二至最大值进行比较)。

Another option is to sort the array and just return the last 2 elements. 另一个选择是对数组进行排序,只返回最后2个元素。 There's Arrays.sort which can be helpful. Arrays.sort可能会有所帮助。

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

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