我正在尝试将两个链表list1和list2合并在一起。在list2中,你有一个在list1中不可用的项目,它是键盘,鼠标的数量也发生了变化,新的鼠标数量将是3,因为list1中有1个鼠标数量,list2中有3个。所以本质上,你会有一个新的链表,比如list3。list3将把list1和list2的内容组合在一起,就像list2中但不在list1中的项目键盘一样,还有鼠标的新数量。
import java.util.LinkedList;
import java.util.ListIterator;
public class TestLinkedList extends MyLinkedList {
public static void main(String[] args) {
LinkedList < String > list1 = new LinkedList < > ();
Demo demo = new Demo();
String case1 = demo.setNameandQty(1, "Case");
String monitor1 = demo.setNameandQty(3, "Monitor");
String mouse1 = demo.setNameandQty(1, "Mouse");
String ram1 = demo.setNameandQty(2, "RAM");
String ssd1 = demo.setNameandQty(4, "SSD");
int cm = 2;
list1.add(case1);
list1.add(monitor1);
list1.add(mouse1);
list1.add(ram1);
list1.add(ssd1);
System.out.println("Shopping List 1");
ListIterator < String > it1 = list1.listIterator();
while (it1.hasNext()) {
System.out.println(it1.next());
}
list1.remove(3);
System.out.println("nAfter deleting 4th item");
for (String s: list1) {
System.out.println(s);
}
LinkedList < String > list2 = new LinkedList < > ();
String keyboard2 = demo.setNameandQty(1, "Keyboard");
String mouse2 = demo.setNameandQty(2, "Mouse");
list2.add(keyboard2);
list2.add(mouse2);
System.out.println("nShopping list 2");
for (String s: list2) {
System.out.println(s);
}
String holdMouse1q = "";
String holdMouse2q = "";
String getMouse1q = demo.getIntegers(mouse1, holdMouse1q);
int mouse1q = Integer.parseInt(getMouse1q);
String getMouse2q = demo.getIntegers(mouse2, holdMouse2q);
int mouse2q = Integer.parseInt(getMouse2q);
System.out.println("nMouse 1 quantity: " + mouse1q);
System.out.println("Mouse 2 quantity: " + mouse2q);
int totalMouseQ = mouse1q + mouse2q;
// list1.addFirst("Flashdisks : 10");
// System.out.println("nAfter adding 10 flash disks");
// for (String s : list1) {
// System.out.println(s);
// }
}
}
我不确定方法demo.setNameandQty()
的返回值是什么。所以我只是假设返回值应该像<name>-<quality>
,例如"Mouse-2"。
下面是我的合并方法,通过排序项目列表来解决:
private static List<String> merge(List<String> list1, List<String> list2){
Collections.sort(list1);
Collections.sort(list2);
//ensure list always has 1 element
list1.add(null);
list2.add(null);
List<String> list3 = new LinkedList<String>();
Iterator<String> l1 = list1.iterator();
Iterator<String> l2 = list2.iterator();
String item1 = l1.next();
String item2 = l2.next();
while(item1 != null && item2 != null){
String name1 = getName(item1);
String name2 = getName(item2);
if(name1.compareTo(name2) < 0){
list3.add(item1);
item1 = l1.next();
}
else if(name1.compareTo(name2) > 0){
list3.add(item2);
item2 = l2.next();
}
else if(name1.compareTo(name2) == 0){
//sum up the quality then add to the list
int totalQty = getQuality(item1) + getQuality(item2);
list3.add(name1 + "-" + totalQty);
item1 = l1.next();
item2 = l2.next();
}
}
//add the rest of the list, in case one of two lists still has items
while(item1 != null){
list3.add(item1);
item1 = l2.next();
}
while(item2 != null){
list3.add(item2);
item2 = l2.next();
}
return list3;
}
private static String getName(String s){
return s.substring(0, s.indexOf('-'));
}
private static int getQuality(String s){
return Integer.valueOf(s.substring(s.indexOf('-')+1));
}
这里需要注意的是,如果您在一个列表中有重复项,例如{"鼠标-1"、"鼠标-2"},则此方法将失败。为了解决这个问题,我认为我们最好确保没有重复的项目,而不是让我们的合并方法更加复杂。