简体   繁体   English

排序LinkedList并实现find方法

[英]Sorting a LinkedList and implementing a find method

I have a linked list class that provides a Node class. 我有一个提供Node类的链表类。

I need to implement a sort class to sort out the contents of the linked list class in alphabetical order. 我需要实现一个排序类,以按字母顺序对链接列表类的内容进行排序。

I also need to implement a find method so that the find method will return all the data of the index it matches to. 我还需要实现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.

Then I want to use the find method so that find("Cris Irish") returns : 然后,我想使用find方法,以便find(“ Cris Irish”)返回:

"Cris Irish",2000, "cI@gmail.com" “爱尔兰的克里斯(Cris Irish)”,2000年,“ cI@gmail.com”

And I wanna use a comparator to do it. 我想用比较器来做。 But I have no idea how to pass it as an argument in the method. 但是我不知道如何在方法中将其作为参数传递。

This is my comparator class 这是我的比较班

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

I have a contact1 class that defines the contents if the linked list. 我有一个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);

}
}

Here's the contact class: 这是联系方式:

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;
}

From the look of it, you list has a natural order, so why don't you use a TreeSet instead of a list? 从它的外观看,您的列表具有自然顺序,那么为什么不使用TreeSet而不是列表呢? In a set any entry must be unique and a tree set is ordered. 在集合中,任何条目都必须是唯一的,并且必须对树集进行排序。 Then your Contact1 class could implement Comparable and implement the Comparator itself. 然后,您的Contact1类可以实现Comparable并实现Comparator本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM