[英]Sorting Generic Array Type List With For Loop in Java
我知道如何使用.sort()等内置方法对通用数组类型列表进行排序。但是,我想使用for循环手动对列表进行排序。 有人可以帮我这个方法吗? 这是我的ListClass
package AlgorithmAndDataStructures;
import java.util.Arrays;
public class ListClass<T extends Comparable<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];
}
// Removing / Deleting All Elements of Generic List of Type Array
// Build in Method for sorting
public void sort() {
Arrays.sort(listArray, 0, totalElements);
}
}
在课程结束时,您可以看到有一个内置方法可以排序。 一切正常。 但是,我想使用for循环。 这也是我的驱动程序。
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();
listObjectInt.sort();
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中的更新排序方法
// Bubble Sort
public void bubbleSort(T[] list) {
int n = list.length - 1;
while (n != 0) {
int i;
for ( i = 0; i < n; i++) {
if (( list[i]).compareTo(list[i + 1]) > 0) {
T temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
n= i-1;
}
}
现在,我在驱动程序中对如何调用感到困惑?
listObjectInt.bubbleSort(WHAT_SHOULD_BE_HERE?);
期待很好的帮助! 谢谢!
您可以使用许多排序算法来执行此操作(请参阅https://en.wikipedia.org/wiki/Sorting_algorithm以获取一些较常见的排序算法)。 最简单的方法之一是冒泡排序,它使用包装在while
循环中的for
循环。
我会给你一些伪代码-转换为代码应该相对简单。
while not sorted
for each element in the list after the first one
if the element is larger than the previous one
swap the element with the previous one
我认为您的老师要求您执行的操作类似于冒泡排序或选择排序,并且在您的测试中,如果genericType是String,则排序方法不正确。
如果需要泛型排序,则可以实现Comparable。(非for循环)
如果genericType是Interger,这是正确的:
public void sort() {
//Arrays.sort(listArray, 0, totalElements);
for(int i = 0 ;i<totalElements;i++){
for(int j=0;j<totalElements-i-1;j++){
System.out.println(i+"+"+j);
if((int)listArray[j]>(int)listArray[j+1]){
T temp = (T) listArray[j];
listArray[j] = (T) listArray[j+1];
listArray[j + 1] = temp;
}
}
}
}
我的英语很差,我不确定这是否正是您想要的。
使您的类型T扩展Comparable并使用对象的compareTo方法
您无法比较两个没有任何信息的对象。 它必须实现一些接口。 您可以为此创建自己的界面吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.