java字母排序_java – 使用compareTo()方法按字母顺序对列表进行排序

我正在用

java编写电话簿程序,我需要按字母顺序列出列表中的人员,为此我需要为java中的列表编写排序算法,它应该只使用compareTo()方法.所以有人可以帮助我做到这一点吗?

public void listAlpha()

{

Node tempNode = head;

for(int i = 0; i <= size; i++)

{

for(int j = 0; j <= i; j++)

{

int comparison = ((tempNode.getNext().getElement().getName()).compareTo(tempNode.getElement().getName()));

if(comparison < 0)

{

Person tempPerson = tempNode.getElement();

tempNode.setElement(tempNode.getNext().getElement());

tempNode.getNext().setElement(tempPerson);

tempNode = tempNode.getNext();

}

}

}

(顺便说一下,这是一个家庭作业,我使用自己的数据结构.)

这是我上面写的方法所属的类:

import java.util.*;

/** Singly linked list implementation .*/

public class SLinkedList implements LinkedList, Iterable {

protected Node head; // head node of the list

protected Node tail; // tail node of the list

protected int size; // number of nodes in the list

public Iterator iterator()

{

return new LinkedListIterator(head);

}

/** Default constructor that creates an empty list */

public SLinkedList() {

head = null;

tail = null;

size = 0;

}

public int size() {

return size;

}

public boolean isEmpty() {

return size == 0;

}

public void addFirst(E newElement) {

Node newNode = new Node(newElement,null);

if(size == 0) //if list is empty

tail = newNode;

newNode.setNext(head);

head = newNode;

size++;

}

public void addLast(E newElement) {

Node newNode = new Node(newElement,null);

if(size == 0) //if list is empty

head = newNode;

if (size != 0) //if list is not empty

tail.setNext(newNode);

tail = newNode;

size++;

}

public E removeFirst() {

Node tempNode = null;

if (size != 0) {

if(size == 1)

tail = null;

tempNode = head;

head = head.getNext();

tempNode.setNext(null);

size--;

}

//if list is empty then return null

return tempNode.getElement();

}

public E removeLast() {

Node tempNode = head;

if(size == 0)

return null;

if(size == 1) {

head = null;

tail = null;

size--;

return tempNode.getElement();

}

//size is greater than 1

for(int i=1; i<=size-2; i++) {

tempNode = tempNode.getNext(); //go to element that before the tail

}

Node tempNode2 = tail;

tail = tempNode;

tail.setNext(null);

size--;

return tempNode2.getElement();

}

public void remove(E element){

int index = 0;

boolean found = false;

Node temp = head;

for(int i=1; i<=size; i++) {//find the node with element

index++;

if(temp.getElement().equals(element)){

found = true;

break;

}

temp = temp.getNext();

}

if(found){

if(index == 1)

removeFirst();

else if(index == size)

removeLast();

else{

//find the previous node

Node prev = head;

for(int i=1; i

prev = prev.getNext();

}

prev.setNext(temp.getNext());

temp.setNext(null);

size--;

}

}

}

public int searchList(E searchKey) {

if(size == 0)

return -1;

Node tempNode = head;

for(int i=1; i<=size; i++) {

if(tempNode.getElement().equals(searchKey))

return i; //return index of the node

tempNode = tempNode.getNext();

}

return -1; //not found

}

public void printList() {

Node tempNode = head;

for(int i=1; i<=size; i++) {

System.out.print(tempNode.getElement());

if(i!=size) //if it is not last element

System.out.print(" - ");

tempNode = tempNode.getNext();

}

System.out.println();

}

人员类:

public class Person

{

private String name;

private String surname;

private String address;

private PhoneNumber phone1;

private PhoneNumber phone2;

private PhoneNumber phone3;

public Person()

{

name = null;

surname = null;

address = null;

phone1.setPhone(0);

phone1.setType("");

phone2.setPhone(0);

phone2.setType("");

phone3.setPhone(0);

phone3.setType("");

}

public Person(String n, String s, String a,PhoneNumber p1, PhoneNumber p2, PhoneNumber p3)

{

name = n;

surname = s;

address = a;

phone1 = p1;

phone2 = p2;

phone3 = p3;

}

public String getName()

{

return name;

}

public void setName(String n)

{

name = n;

}

public String getSur()

{

return surname;

}

public void setSur(String s)

{

surname = s;

}

public void insertPhone(PhoneNumber phone)

{

if(phone2 == null)

phone2 = phone;

else if(phone3 == null)

phone3 = phone;

}

public PhoneNumber getPhone1()

{

return phone1;

}

public PhoneNumber getPhone2()

{

return phone2;

}

public PhoneNumber getPhone3()

{

return phone3;

}

public String getAdd()

{

return address;

}

public void setAdd(String a)

{

address = a;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值