【发布时间】:2017-09-09 10:13:29
【问题描述】:
我有两个 LinkedList:newLinkedList 和 oldLinkedList,它们都包含 BID 类对象。以下是我的 BID 课程:
public class Bid {
private int quantity;
private double bidPrice;
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getBidprice() {
return bidPrice;
}
public void setBidprice(double bidPrice) {
this.bidPrice = bidPrice;
}
}
现在我必须创建一个新的 LinkedListlist,其中包含基于 BID 类的价格变量的排序元素 newLinkedList 和 oldLinkedList。 如果我在两个 LinkedList 中得到相同的价格,那么我必须保留 newLinkedList BID 类对象并删除旧的。
这意味着新的 LinkedList 必须包含根据价格变量排序的 Bid 类对象。
这是我的主要功能:
public static void main(String[] args) throws InterruptedException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter size of linkedlist 1 ");
int size1 = Integer.parseInt(br.readLine());
System.out.println("Enter size of linkedlist 2 ");
int size2 = Integer.parseInt(br.readLine());
LinkedList<Bid> oldLinkedList= addElementsToList(size1);
LinkedList<Bid> newLinkedList= addElementsToList(size2);
/*
SORT BOTH THE LINKED LISTS HERE
*/
}
public static LinkedList<Bid> addElementsToList(int size) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
LinkedList<Bid> bidList = new LinkedList<Bid>();
for (int i = 0; i < size; i++) {
Bid bid = new Bid();
System.out.println("Enter bid price of Object " + i);
bid.setBidprice(Double.parseDouble(br.readLine()));
System.out.println("Enter bid quantity of Object " + i);
bid.setQuantity(Integer.parseInt(br.readLine()));
bidList.add(bid);
}
return bidList;
}
【问题讨论】:
-
你有两个链表..然后使用嵌套的for循环,比较器或类似的东西
-
你有什么问题?
-
"将两个链表合并为一个,并对最终链表进行排序。"但是 LinkedList 包含“Bid”类对象。所以必须根据“Bid”类对象的价格变量进行排序。
标签: java sorting object linked-list