有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java排序LinkedList并实现find方法

我有一个链表类,它提供了一个节点类

我需要实现一个排序类来按字母顺序对链表类的内容进行排序

我还需要实现一个find方法,这样find方法将返回它所匹配的索引的所有数据

list.addFirst(new Contact1("John Harvey" ,6000, "jh@gmail.com"));
list.addFirst(new Contact1("Cris Irish",2000, "cI@gmail.com"));
"John Harvey" and "Cris Irish" are the names. I want to compare Cris Irish John Harvey and arrange them in alphabetical order.

然后我想使用find方法,以便find(“Cris Irish”)返回:

“克里斯爱尔兰”,2000年cI@gmail.com"

我想用比较器来做。但是我不知道如何在方法中将其作为参数传递

这是我的比较器类

public class ContactComparator implements Comparator<Contact1>{
@Override
 public int compare(Contact1 a, Contact1 b)
{
    return b.getName().charAt(0)-(a.getName().charAt(0));
}
}

我有一个contact1类,它定义了链接列表中的内容

import java.util.Collections;
import java.util.Comparator;
import java.util.NoSuchElementException; 


package project3;


import java.util.Collections;
import java.util.Comparator;
import java.util.NoSuchElementException

public class LinkedList
{
private static Node first;
private int currentSize;
private static LinkedList list = new LinkedList();
/**
 * Constructs an empty linked list
 */
public LinkedList()
{
    first = null;
    currentSize=0;

}

/**
 * Returns the first element in the linked list.
 * @return the first element in the linked list
 */
public Object getFirst()
{
    return first;
}

/**
 * Removes the first element in the linked list.
 * @return the removed element
 */
public Object removeFirst()
{
    if (first == null) { throw new NoSuchElementException(); }
    Object element = first.data;
    first = first.next;
    currentSize--;
    return element;
}

/**
 * Adds an element to the front of the linked list.
 * @param the element to add
 */
public void addFirst(Object element)
{
    Node newNode = new Node();
    newNode.data = element;
    newNode.next = first;
    first = newNode;
    currentSize++;
}

/**
 * A linked list node, holding the data (an object) and a pointer to
 * the next node in the list.
 */
class Node
{
    public Object data;
    public Node next;
    }


/** Reverses Contents in the linked List */  
public void reverse()   {           
    if (first ==null)   {return;}
    Node primary = first;
    Node current = primary;
    Node previous = null;
    while (current!= null)  {
        Node next = current.next;
        current.next= previous;
        previous = current;
        current = next;
    }
    first = previous;
}

/** Gives the size of the linked List*/
public void size()  {
    Node element = first;
    int elementIndex = 0;
    if (first == null)  {System.out.println(0);}
    else {
        while (element!= null){
        element = element.next;
        elementIndex++;}
        }
    System.out.println(elementIndex);
    }
/**  Helper method for get(int n) method */
private Node getNode(int n) {
    int index;
    Node element = first;
    for (index =0; index < n; index++)  {
        element = element.next;
    }
    return element;
}

/** Returns the nth element in the linked list */
public Object get(int n)    {
    return getNode(n).data;     
}

/** Sorts the linked List*/
public  void sort(Node node)        {

}



public Object findElement(Object element){
    }

public void print(Node node)    {
    if (node == null)   {node =first;}
while (node!= null) {
    System.out.println(node.data);
    node = node.next;
}
}

public class ContactComparator implements Comparator<Contact1>{
    @Override
     public int compare(Contact1 a, Contact1 b)
    {
        return b.getName().charAt(0)-(a.getName().charAt(0));
    }
}

public static void main (String [] args)    {
    list.addFirst(new Contact1("John Harvey" ,6000, "jh@gmail.com"));
    list.addFirst(new Contact1("Cris Irish",2000, "cI@gmail.com"));
    list.addFirst(new Contact1("Tom Jhonson",2400,"tj@gmail.com" ));
    System.out.println("Current List:");
    list.print(null);
    list.reverse();
    System.out.println("Reverse List: ");
    list.print(null);
    System.out.println("Size of the list");
    list.size();
    System.out.println("Current Size of the List");
    System.out.println(list.currentSize);   
    list.get(1);

}
}

以下是contact类:

package project3;

public class Contact1 {

private String name;
private int number;
private String mail;

public Contact1(String n, int pn, String e){
    this.name= n;
    this.number = pn;
    this.mail =e;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}


public int getNumber() {
    return number;
}
public void setNumber(int number) {
    this.number = number;
}

public String getEmail() {
    return mail;
}
public void setEmail(String name) {
    this.mail = name;
}
public String toString(){
    return "Name: "+this.name+" "+" Number "+this.number+" Email: "     +this.mail;
}

共 (1) 个答案

  1. # 1 楼答案

    从外观上看,列表有一个自然的顺序,那么为什么不使用TreeSet而不是列表呢?在集合中,任何条目都必须是唯一的,并且树集合是有序的。然后您的Contact1类可以实现Comparable并实现Comparator本身