用泛型对整数数组进行排序(Java)

编程入门 行业动态 更新时间:2024-10-28 18:33:02
本文介绍了用泛型对整数数组进行排序(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是计算机科学的二年级学生,目前正在使用Java进行工作,我们最近开始了泛型的研究.我有一个作业,在其中给了我一个使用泛型的排序算法列表,并负责使用它们对整数(不是原始int)列表进行排序.由于排序类使用可扩展Comparable的泛型,所以我认为将它们传递给Integer数组不会有问题,但是构建输出会不断出现不兼容的类型.

I'm a second year computer science student currently working in Java and we recently started generics. I have an assignment where I've been given a list of sorting algorithms that use generics and am tasked with using them to sort a list of Integers (not primitive ints). As the sort classes use generics that extend Comparable I thought there would be no problems simply handing them the Integer array but the build output keeps coming up with incompatible types.

相关代码如下;

主程序的一部分

final int NUMITEMS = 100000; Integer[] list = new Integer[NUMITEMS]; int dataSize = 0; //method reads contents of a file into array and returns number of objects System.out.println((dataSize = readDataFile(list))); SelectionSort SS = new SelectionSort(list, dataSize);//problem is here

所提供的SelectionSort算法以及预期将原样使用的算法

And the SelectionSort algorithm which is provided and expected to be used as-is

class SelectionSort<T extends Comparable<? super T>> implements SortAlgorithm<T> { public void sort ( T [ ] theArray, int size ) { for (int last = size-1; last > 0 ; last--) { int largest = 0; for (int scan = 1; scan <= last; scan++) if (theArray[scan]pareTo(theArray[largest])>0) largest = scan; /** Swap the values */ T temp = theArray[largest]; theArray[largest] = theArray[last]; theArray[last] = temp; } } // method selectionSort

我遇到的问题是在声明SelectionSort时,它返回一个错误,即构造函数无法应用于给定类型.从我在这里和其他地方的搜索中所阅读的内容来看,在使用int时通常会遇到这种问题,但是我不明白为什么它不适用于Integers.对问题的任何见解将不胜感激,因为我仍然会接受泛型的概念.提前非常感谢!

The problem I'm having is in declaring SelectionSort, which returns an error that the constructor cannot be applied to the given type. From what I've read in my searches here and elsewhere this kind of problem is usually encountered when using ints, but I don't understand why it isn't working with Integers. Any insight on the problem would be greatly appreciated as I'm still coming to terms with the concept of generics. Many thanks in advance!

推荐答案

这应该可以解决问题:

SelectionSort<Integer> ss = new SelectionSort<Integer>(); ss.sort(list, dataSize);

当您想将参数传递给 sort 方法时,您试图将参数传递给不存在的构造函数.

You were trying to pass arguments into a constructor that didn't exist, when you want to pass them into the sort method instead.

这里我使用默认的(无参数)构造函数实例化一个新的 SelectionSort< Integer> ,将其分配给变量 ss ,然后调用 sort .

Here I'm using the default (no-arg) constructor to instantiate a new SelectionSort<Integer>, assigning it to a variable ss, then calling sort on that instance with the arguments.

还请注意,如果您只需要实例来调用 sort ,则可以跳过分配:

Also note that if you just need the instance to call sort, you can skip the assignment:

new SelectionSort<Integer>().sort(list, dataSize);

更多推荐

用泛型对整数数组进行排序(Java)

本文发布于:2023-11-30 14:41:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   数组   用泛型   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!