正如我的问题所描述的,如何在java中对类型数组列表进行排序?
这是我的ListClass
package AlgorithmAndDataStructures;
public class ListClass<T>{
private static final int MAX_SIZE_OF_LIST = 100;
/**
* This class is having definitions for:-
* Generic List Class Variables
* Constructor for Creating Class Objects
* Method: Adding a Element to the End of List
* Method: Adding a Element at anywhere/ particular place
* Method: Checking List is full or not.
* Method: Checking List is Empty or Not.
* Method: Displaying All Elements of List
* Method: Making New Space for new element addition.
* Method: Sorting a List
*/
// Declaring Array and Variables
private T[] listArray;
private int totalElements;
// Constructor For ListClass
@SuppressWarnings("unchecked")
public ListClass(int listSize) { // entered by the user on runtime
totalElements = 0;
listArray = (T[]) new Object[listSize];
}
// Method For Adding an Element
public boolean addElement(T newElement)
{
boolean isElementAdded = true;
if(!isListFull()) {
listArray[totalElements] = newElement;
totalElements++;
}
else
System.out.println("Sorry, the list is full so, new element can not be added.");
isElementAdded = false;
return isElementAdded;
}
// length = totalElements
// Method for Adding/Inserting Element in any Particular Place
public boolean addSpecific(int newLocation, T newElement) {
boolean elementAdded = true;
if (!isListFull() && (newLocation >= 1) && (newLocation <= totalElements +1) )
{
newSpace(newLocation);
listArray[newLocation -1] = newElement;
totalElements++;
}
else {
elementAdded = false;
}
return elementAdded;
}
// Method for Displaying The List Elements
public void displayListElement() {
if(isListEmpty())
{
System.out.println("Sorry, there is no element in the List!");
}
else
{
for(int elements = 0; elements < totalElements; elements++ ) {
System.out.println((listArray[elements]));
}
System.out.println("All elements has been displayed!");
}
}
// Method for Checking if List is Empty or Number of elements = 0
public boolean isListEmpty() {
return totalElements == 0;
}
// Method for Checking is List is full or not.
public boolean isListFull()
{
return totalElements == MAX_SIZE_OF_LIST;
}
private void newSpace( int newLocation)
{
// assert is a method predefined; indicator for index number
assert (newLocation >=1) && (newLocation <= totalElements +1);
int newIndicator = newLocation -1;
int lastIndicator = totalElements -1;
/**
* For Shifting Elements to Next Indexes
*/
for ( int sign = lastIndicator; sign >= newIndicator; sign--)
listArray[sign +1] = listArray[sign];
}
}
这是我的DriverProgram
package AlgorithmAndDataStructures;
public class DriverListClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
ListClass<Integer> listObjectInt = new ListClass<Integer>(10);
listObjectInt.addElement(12);
listObjectInt.addElement(17);
listObjectInt.addElement(90);
listObjectInt.addElement(53);
listObjectInt.addSpecific(3, 56);
listObjectInt.displayListElement();
// String List
ListClass<String> listObjectStr = new ListClass<String>(4);
listObjectStr.addElement("Suman");
listObjectStr.addElement("Armaan");
listObjectStr.addElement("Atif");
listObjectStr.addElement("Tauseef");
listObjectStr.displayListElement();
}
}
现在,我想在 ListClass 中使用通用排序方法,它可以对通过驱动程序生成的任何类型(String,Integer,Double,Float等)的列表进行排序。
答案 0 :(得分:1)
向您的班级添加排序方法
public void sort() {
Arrays.sort(listArray, 0, totalElements);
}
将class ListClass<T>
更改为class ListClass<T extends Comparable<T>>
。
如果您想按特定的比较器进行排序,请添加以下方法。
public void sort(Comparator<T> comparator) {
Arrays.sort(listArray, 0, totalElements, comparator);
}
答案 1 :(得分:0)
您需要为列表的元素定义比较方法。这通常通过使元素实现像Comparable
这样的接口来完成:
public class ListClass<T extends Comparble<T>>