java中如何插入字母并排序_java插入並在節點列表中按字母順序排序

我試圖創建一個程序,可以按字母順序對所提供的「雜誌標題」列表進行排序,並在插入新雜誌時刷新排序列表。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()方法的東西 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值