我試圖創建一個程序,可以按字母順序對所提供的「雜誌標題」列表進行排序,並在插入新雜誌時刷新排序列表。java插入並在節點列表中按字母順序排序
以下是具體的作業指導: 修改雜誌架程序,以按字母順序添加新雜誌(您必須修改雜誌以使其與compareTo()方法等同)。
獎勵:實施一種刪除方法,並在驅動程序中進行演示。
這裏是我到目前爲止我的列表:
public class MagazineList_jcm
{
private MagazineNode list;
//----------------------------------------------------------------
// Sets up an initially empty list of magazines.
//----------------------------------------------------------------
public MagazineList_jcm()
{
list = null;
}
// public boolean delete(Magazine mag)
// {
// MagazineNode current = list;
// MagazineNode previous = null;
//look through list of magazines
//first, check for a null list
// if so, return false
//if a current.mag with the same title as mag
//delete it from the list
//if no mag found, return false
// }
//----------------------------------------------------------------
// Creates a new MagazineNode object and adds it to the end of
// the linked list.
//----------------------------------------------------------------
public void add(Magazine_jcm mag)
{
MagazineNode node = new MagazineNode(mag);
MagazineNode current;
MagazineNode previous;
boolean done = false;
if (list == null)
list = node;
else
{
current = list;
while (!done)
{
int comp = current.magazine.compareTo(mag);
if (comp == 0)
//- duplicate
else if (comp > 0)
//add before current
if previous null
//insert at front of list
list = node;
node.next = current;
//ELSE, INSERT BETWEEN PREVIOUS AND CURRENT
// else if (comp <0)
//add after....
//if current.next null
//add right after
// else
//move to the next node
previous = current;
current = current.next;
current = current.next;
}
current.next = node;
}
}
//----------------------------------------------------------------
// Returns this list of magazines as a string.
//----------------------------------------------------------------
public String toString()
{
String result = "";
MagazineNode current = list;
while (current != null)
{
result += current.magazine + "\n";
current = current.next;
}
return result;
}
//*****************************************************************
// An inner class that represents a node in the magazine list.
// The public variables are accessed by the MagazineList class.
//*****************************************************************
private class MagazineNode
{
public Magazine magazine;
public MagazineNode next;
//--------------------------------------------------------------
// Sets up the node
//--------------------------------------------------------------
public MagazineNode(Magazine mag)
{
magazine = mag;
next = null;
}
}
}
這裏是我的 「雜誌」:
public class Magazine_jcm
{
private String title;
//-----------------------------------------------------------------
// Sets up the new magazine with its title.
//-----------------------------------------------------------------
public Magazine_jcm(String newTitle)
{
title = newTitle;
}
//-----------------------------------------------------------------
// Returns this magazine as a string.
//-----------------------------------------------------------------
public String toString()
{
return title;
}
}
這裏是我的驅動程序:
public class MagazineRack_jcm
{
//----------------------------------------------------------------
// Creates a MagazineList object, adds several magazines to the
// list, then prints it.
//----------------------------------------------------------------
public static void main(String[] args)
{
MagazineList_jcm rack = new MagazineList_jcm();
rack.add(new Magazine_jcm("Time"));
rack.add(new Magazine_jcm("Woodworking Today"));
rack.add(new Magazine_jcm("Communications of the ACM"));
rack.add(new Magazine_jcm("House and Garden"));
rack.add(new Magazine_jcm("GQ"));
String newMag = JOptionPane.showInputDialog("Please enter a Magazine to add to the list:");
rack.add(new Magazine_jcm(newMag));
System.out.println(rack);
}
}
2015-07-21
jaxicab
+2
請問一個具體的問題。 –
+0
你應該看看這個https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html –
+0
我在問如何完成插入一個新的雜誌對象的任務,並與給定的排序雜誌對象使用類似於compareTo()方法的東西 –