- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在用 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<E> implements LinkedList<E>, Iterable<E> {
protected Node<E> head; // head node of the list
protected Node<E> tail; // tail node of the list
protected int size; // number of nodes in the list
public Iterator<E> 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<E> 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<E> 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<E> 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<E> 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<E> tempNode2 = tail;
tail = tempNode;
tail.setNext(null);
size--;
return tempNode2.getElement();
}
public void remove(E element){
int index = 0;
boolean found = false;
Node<E> 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<E> prev = head;
for(int i=1; i<index-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;
}
最佳答案
您可以让您的Person
类实现Comparable
,并定义以下方法:
public class Person implements Comparable<Person> {
// Your previous code
public int compareTo(Person other) {
if (other == null) {
// throw exception for example
}
return this.name.toLowerCase().compareTo(other.name.toLowerCase());
}
}
关于java - 使用 compareTo() 方法按字母顺序对列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27410241/
这似乎很奇怪,这并没有像我预期的那样工作。我编写了一个简单的java类,它实现Comparable接口(interface)并重写compareTo()方法。但是,它不允许我传递除对象之外的特定类型的
public int compareTo(Object another) throws CustomMadeException { if(this.getClass() != another.
早些时候,我在通用 IComparable 中获得了逆变的具体示例Jon Skeet 的界面。然而,这又产生了另一个问题。为什么不是通用的 List.Sort()方法能够推断出相同的信息? 我在这里提
我有注释 package javaannotationtest; import java.lang.annotation.*; @Target({ElementType.METHOD}) @Reten
我试图理解 C# 中的 CompareTo(),下面的示例让我比以往任何时候都更加困惑。有人能帮我理解为什么第三个变体的结果是 1 吗?句子“Hello wordd”中的第二个词与 str1“Hell
我已经在这个程序上工作了几天,并且在我的 BinarySearchTree 类中实现了一些主要方法,例如插入和删除。插入似乎工作正常,但是一旦我尝试删除,我就会不断出错。因此,在玩弄了代码之后,我想测
首先,我必须提到的是,在阅读了大量问题和教程并观看了一些视频后,问题仍然没有解决。 我是一名Java中级程序员,我编写了一些用于比较优先级队列中的元素的代码,其中元素的保存方式类似于[Comparab
我只是在为即将到来的考试做一些复习。我发现了我们的讲师给我们提供的这段代码供我们修改。 代码: public class Employee implements Comparable{ private
我的书要求我为一段代码编写 Javadoc 注释。大多数情况下,我了解如何执行 javadocs,但我不了解该程序在做什么。 “为类 Person 的以下方法编写 Javadoc 注释。假设类 Per
如果我写以下内容是否有可能溢出: public class SomeObj implements Comparable { private final float data; pub
我有一个名为任务的类,我想将其放入 PriorityQueue 中。 我的类(class)通过日期和名为isUrgent的 boolean 字段进行比较 @Override publ
这个问题已经有答案了: Java error: Comparison method violates its general contract (13 个回答) 已关闭 7 年前。 我有这个compa
我的类(class)结构: public class Priorityy implement Comparable { public int compareTo(Object pe) {
给定一个非负整数列表,我想对它们进行排列,使它们形成最大的数字。给定 [1, 20, 23, 4, 8],最大的形成数字是 8423201。但我想弄清楚compareTo 方法中变量的顺序如何影响 A
我想通过上次联系日期比较两个“收件人”,如果相同,则通过地址进行比较。这是我的代码: public class RecipientComparator implements Comparator {
package Comparable; public class Movie implements Comparable { private double rating; privat
您好,我在实现compareTo 方法时遇到问题。我一直在寻找答案,但没有任何帮助。我正在尝试用各种大小的圆圈填充 TreeSet。我需要在我的圈子类中使用compareTo 才能以这种方式存储它们。
我有以下代码;目的是返回数组中按字母顺序排列的最小成员。 public String smallest() { String smallest = ""; int i = 0; while(log[i
我有一个关于compareTo函数如何协助比较器进行排序的问题即 o1.compareTo(o2) 与 o2.compareTo(o1) 如果两个字符串相等,则此方法返回 0,否则返回正值或负值。如果
帮助我无法弄清楚compareTo函数。这就是我必须做的:编写一个compareTo函数,可用于根据以下内容按顺序放置产品到他们的零件号。也就是说,后面按字母顺序排列的零件号顺序大于按字母顺序排列较早
我是一名优秀的程序员,十分优秀!