我知道如何使用 .sort() 等内置方法对通用数组类型列表进行排序。但是,我想使用 for 循环手动对列表进行排序。谁能帮助我使用这个方法? 这是我的列表类
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);
}
}
在类(class)结束时,您可以看到有一个内置的排序方法。它运行良好。但是,我想使用 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
关于java - 在 Java 中使用 For 循环对通用数组类型列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35567546/