2016-09-03 86 views
1

我有一个用于排序我们先前使用插入排序方法创建的LinkedList的类。我们通过阅读Excel文件并列出5个贡献者来创建列表。我意识到这听起来像一个重复的问题......但是,我可以找到的所有样本处理整数或数组,我没有找到处理字符串或使用像我正在使用的LinkedList。另一个问题是,我发现处理不仅仅是整数的例子假设你使用头和节点以及类似的东西从头开始创建列表......正如你在我的代码中看到的那样,我没有使我的从头开始,我只是使用Java实用程序中的构建来制作我的。无论如何,我的代码可能并不是超高效的,但是到目前为止,我的任务已经达到了100个,所以对于我认为的学校来说这已经足够了,但是任何改进它的建议都是值得欢迎的。我是编程的初学者,只有经历我有以前的课程。所以,这里是我的代码:使用Java中的插入排序方法对链表排序

import java.io.*; 
import java.util.*; 

public class ChrisJohnson_Unit3_IP { 

static class Contributor{ //create class to store contributor information 
    //declare variables 
    private String firstName; 
    private String lastName; 
    private String country; 
    private String phone; 
    private double contribution; 
    private int id; 

    //methods for setting variable values 
    public String getFirstName(){ 
     return firstName; 
    } 

    public void setFirstName(String firstName){ 
     this.firstName = firstName; 
    } 
    public String getLastName(){ 
     return lastName; 
    } 

    public void setLastName(String lastName){ 
     this.lastName = lastName; 
    } 

    public String getCountry(){ 
     return country; 
    } 

    public void setCountry(String country){ 
     this.country = country; 
    } 

    public String getPhone() { 
     return phone; 
    } 

    public void setPhone(String phone){ 
     this.phone = phone; 
    } 

    public double getContribution(){ 
     return contribution; 
    } 

    public void setContribution(double contribution){ 
     this.contribution = contribution; 
    } 

    public int getId(){ 
     return id; 
    } 

    public void setId(int id){ 
     this.id = id; 
    } 

    public void Print(){//method to print class objects 
     System.out.printf("%-10s %-10s %-8s %-15s %s %-15s %d %n", firstName,  lastName, country, 
     phone, "$", contribution, id); 
    } 
}//end Contributor class 

static LinkedList contributorList = new LinkedList(); //create new Contributor Linked List 
static Hashtable<String, Contributor> memberID = new Hashtable<>();//create new Hash Table 

public static void main(String[] arg) throws Exception { 

String response; 
String ID; 

Contributor contributorData = null; 


Scanner in = new Scanner(System.in); 

//print Welcome message and describe program to user 
System.out.println("Welcome! This program will read your contributors.csv file " 
     + "and store it into a list. \nTThe program will then sort the list and" 
     + "print it for you to view/n"); 

System.out.println("Press enter to read the currently saved contributors.csv file..."); 
in.nextLine(); 

BufferedReader File = 
    new BufferedReader(new FileReader("contributors.csv")); 

String dataRow = File.readLine(); // Read first line. 
// The while checks to see if the data is null. If 
// it is, end of file has been reached. If not, 
// data will be processed. 

while (dataRow != null){//While to read contributors.csv file and store in Contributor object 

String[] data = dataRow.split(","); 
contributorData = new Contributor(); //create new Contributor object 

//store data into Contributor object 
    contributorData.setFirstName(data[0]); 
    contributorData.setLastName(data[1]); 
    contributorData.setCountry(data[2]); 
    contributorData.setPhone(data[3]); 
    contributorData.setContribution(Double.parseDouble(data[4])); 
    contributorData.setId(Integer.parseInt(data[5])); 
    ID = Integer.toString(contributorData.getId()); 
    contributorList.push(contributorData);//add object to top of contributorList 

    memberID.put(ID,contributorData);//add contributor ID to key element of Hash Table 
    dataRow = File.readLine(); // Read next line of data. 
}//end While to read contributors.csv file 

File.close();//close CSV file 

System.out.println("Here is your unsorted contributor list:\n"); 
//call Print method to print the list 
System.out.printf("%-10s %-10s %-8s %-15s %-17s %s %n", "First", "Last", 
     "Country", "Phone #", "Contribution", "ID"); 
Iterator<Contributor> iter = contributorList.iterator(); 
while(iter.hasNext()){ 
    iter.next().Print(); 
}//end while 

System.out.println("Thank you for using this program!"); 
} //main() 

}//end ChrisJohnson_Unit3_IP class 

此外,列表必须使用插入排序方法按名称排序。我理解排序方法的基本概念,但老实说,我不知道如何在这里实现它。我不想找人帮我做作业,只是给我一个正确的方向。任何帮助将不胜感激,如果你需要更多的信息,请让我知道。这个任务是在星期一到期的,所以希望有人能够帮助我。是的,我已经写了我的老师寻求帮助,我整个星期都在城外,所以我一直在努力追赶。感谢您花时间阅读我的问题

+0

为什么?这项任务是愚蠢的。只有疯子才会对链表进行排序。 – EJP

+0

完全同意......这个班级完全被推迟,任务令人沮丧,因为现实世界中没有人会做我们正在学习的任何东西......但不幸的是,我必须这样做:/要离开一个当这个课程结束时,“很好”的评论... –

回答

1

首先,我应该说,在链表上做插入排序是毫无意义的。其次,如果你添加一个getName方法来连接贡献者的名字和姓氏(你可以在排序时进行连接,但是你的代码会变得更加混乱),你可以做如下的事情。

for(int i = 1; i < contributorList.size(); i++) 
{ 
    int j = i; 
    Contributor tmp; 
    while(j > 0 && contributorList.get(j-1).getName().compareTo(contributorList.get(j).getName()) > 0) 
    { 
     tmp = contributorList.remove(j); 
     contributorList.add(j-1, tmp); 
     j = j - 1; 
    } 
} 
+0

那么这比我预期的要容易得多...非常感谢你!我只使用了我在Contributor类中已有的.getFirstName方法,并且工作得很好。这种感觉很愚蠢,因为没有看到这个解决方案......再次感谢,真的为我节省了很多时间! –

+0

没问题,不客气。 – uoyilmaz

+0

这只是毫无意义的,如果你不知道如何正确使用LinkedList,[看我的回答](http://stackoverflow.com/a/39307850/2991525)和顺便说一句,你的Bubble Sort运行在' O(n³)'为'LinkedList',它不比插入排序好。 – fabian

1

首先将您的contributorList更改为使用其保存的对象的Generic类型。那是LinkedList<Contributor>;。其次改变Object来实现Comparable。即class Contributor implements Comparable<Contributor>并实施方法public int compareTo(Contributor other)。第三,选择一种排序方法,并使用compareTo来实现它,以比较排序对象。

0

使用ListIterator找到插入元素和插入的正确点。这使您可以比使用O(n³)运行的“标准方法”更有效地进行插入排序,因为对于索引igetsetO(i)中运行。 (内循环将在O(i²)中运行,因为O(1+2+...+i) = O(i²)O(1²+2²+...+n²) = O(n³))。

注意using a Iterator足以找到插入点在O(n),实现O(n²)运行时间,但使用ListIterator让你找到并插入元素以及删除元素的外部循环迭代的下一个迭代只单个迭代器,如果巧妙地使用。


使用Comparator按照指定的标准进行比较的对象也允许你指定一个标准来排序:

return value of comparator.compare(a, b) |  meaning 
----------------------------------------------------------- 
      0        |  a == b 
     > 0        |  a > b 
     < 0        |  a < b 

在java中8,你可以很容易地创建一个Comparator给出的方法参照方法返回排序标准给予对象:

Comparator<Contributor> comparator = Comparator.comparing(Contributor::getLastName); 

不使用方法引用这可以使用完成实现Comparable接口,像String一个对象的:

Comparator<Contributor> comparator = new Comparator<Contributor>() { 

    @Override 
    public int compare(Contributor o1, Contributor o2) { 
     return o1.getLastName().compareTo(o2.getLastName()); 
    } 

}; 

您使用the Strategy Pattern的排序关系,允许你通过传递不同的Comparator s到使用不同的分类法,这种方法。这也是Collections课程允许您将List与任意内容排序的方式,请参阅Collections.sort(List, Comparator)