永发信息网

关于java arraylist的题

答案:5  悬赏:0  手机版
解决时间 2021-02-28 09:41
  • 提问者网友:萌卜娃娃
  • 2021-02-28 00:30
没例题是在不会做,大家帮帮忙~
You are to define a class PQ of priority queues. Your PQ should have the following methods:

add( String x ) // inserts x into a list kept in sorted order
remove() // removes the first String and returns it
size() // returns the number of items remaining in the PQ.
toString() // returns a String showing the sorted list and
// The elements of the list should be separated by commas and spaces.
In case of a tie the remove method returns the value which has been around the longest, i.e., it behaves like a queue for equal elements. Internally your PQ should store the data in a linked list of Node objects. Linked lists are covered in your instructor's web page on linked lists in Java.

Your main method should test your PQ class by taking command line arguments and adding them to a PQ and then removing them and printing them in ascending order. See command line arguments.

This program will be very short. It will be review for many students. Your program above should be in one file PQ.java.

You would run this program with a command such as java PQ cat ape dog bat .

A sample main program might be:

public static void main( String argv[] ) {
PQ pq = new PQ();
for ( int i = 0; i < argv.length; i++)
pq.add( argv[i] );
System.out.println( pq ); //test toString()
while ( pq.size() > 0 )
System.out.println( pq.remove() );
}

Now if we run java PQ bat cat ape the output would be

ape, bat, cat // from toString()
ape
bat
cat

Note that there is NO Scanner and the methods are NOT static (except main).
Note that the objects to be stored in PQ are of type String. Later we might change that to be any 'Comparable' object.

You should have a separate directory for each course and within that a separate directory for each assignment. For this assignment you should have file PQ.java. together with the matching PQ.class file.

log in to bluenose.cs.dal.ca.
use pico PQ.java to edit your program
use javac PQ.java to compile your program
use java PQ cat bat emu dog ape to run your PQ.class program
Use the command ~prof2110/a0/mark to have your program marked. This program will look for the files mentioned above.

Note - in file PQ.java you can one public class (PQ) and any number of non-public classes such as class Node. This means that Node objects can be used by PQ objects but not by anything else.

Later we will see how to do the same problem more efficiently with a heap.
最佳答案
  • 五星知识达人网友:神也偏爱
  • 2021-02-28 01:18
class Node {
String name; Node next;
public Node( String s, Node n) { name = s; next = n; }
}

public class PQ {
Node n;
int size;

public PQ(){
n=null;
size=0;
}

public void add(String s){
if(n==null) n=new Node(s,null);
else{
if(s.compareTo(n.name)<0) n=new Node(s,n);
else{
Node l=n,m=n.next;
while(m!=null){
if(s.compareTo(m.name)>=0){
l=m;
m=m.next;
}
else break;
}
Node k=new Node(s,m);
l.next=k;
}
}
size++;
}

public String remove(){
if(n==null) return null;
String s=n.name;
n=n.next;
size--;
return s;
}

public int size(){
return size;
}

public String toString(){
if(n==null) return null;
String s="";
Node m=n;
while(m!=null){
s=s+m.name;
m=m.next;
if(m!=null) s=s+", ";
}
return s;
}

public static void main(String[] args) {
PQ p=new PQ();
for(int i=0;i0)
System.out.println(p.remove());

}
}
全部回答
  • 1楼网友:思契十里
  • 2021-02-28 04:12
1、这段代码是没问题的,我试过了。 2、“类型 list 中的方法 add(int, object)对于参数(int)不适用”,你是在什么地方看到的,java里好像没有中文信息。这句话没完全看懂,详细说明一下add方法的用法吧。 list的add(int index, object obj)方法是在指定的索引index的位置插入一个对象obj,原index处和其后的对象依次后移一位。指定的index最大可以是list.size(),也就是最多可以把obj放到list的最后。index 〉 list.size()的时候会引发异常。 方法的第一个参数类型是int,第二个是object。jdk1.4和更早的版本里,第二个参数必须是object。add(0,1)这种写法是错误的,必须用add(0,new integer(1))。从jdk1.5开始,java加入一个重要的机制:自动拆装箱,简单的理解就是基本数据类型和其封装类的自动转化。现在add(0,1)这种写法是没有错误的。
  • 2楼网友:轻熟杀无赦
  • 2021-02-28 04:06
你想要干什么呢
  • 3楼网友:白昼之月
  • 2021-02-28 03:50
对于这样的问题。我只能是打酱油的。
  • 4楼网友:北方的南先生
  • 2021-02-28 02:44
翻译一下?
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯