Java汇总2列表并存储在列表中

时间:2014-04-25 18:30:05

标签: java

public static list Sum2List(list L1, list L2) {
    list l = new list();
    l.first = new listNode(0, null);
    listNode x = l.first;
    for (listNode p = L1.first, p1 = L2.first; (p != null && p1 != null); p = p.next, p1 = p1.next) {
        x.data = p.data + p1.data;
        x.next = new listNode(0, null);
    }
    return l;
}

it shows some error and i think their is error in my coding too.plz if anyone can help out.guys in output its jst showing two values

是否可以使用while循环??

将这些方法代码写入汇总列表

3 个答案:

答案 0 :(得分:1)

这是你的代码,是为了工作。这通常不是你在java中做的,这看起来更像是像perl这样的脚本语言。

public class SomeLists {

    public static void main(String[] args) {
        list l1 = new list();
        l1.first = new listNode();
        l1.first.data = 1;
        l1.first.next = new listNode();
        l1.first.next.data = 2;

        list l2 = new list();
        l2.first = new listNode();
        l2.first.data = 3;
        l2.first.next = new listNode();
        l2.first.next.data = 4;

        list sum = SomeLists.Sum2List(l1, l2);
        assert (sum.first.data == 3);
        assert (sum.first.next.data == 6);
    }

    public static list Sum2List(list L1, list L2) {
        list l = new list();
        listNode currentNode = null;
        for (listNode p1 = L1.first; p1 != null; p1 = p1.next) {
            for (listNode p2 = L2.first; p2 != null; p2 = p2.next) {
                listNode newNode = new listNode();
                newNode.data = p1.data + p2.data;

                if (currentNode == null) {
                    // first time only
                    l.first = newNode;
                }
                else {
                    // all other times
                    currentNode.next = newNode;
                }
                // move currentNode forward.
                currentNode = newNode;
            }
        }
        return l;
    }

    private static class list {
        public listNode first;
    }

    private static class listNode {
        public listNode next;
        public int data;
    }
}

如果你想让它更像Java,那么它应该是面向对象的,成员变量应该是私有的,并通过getter和setter访问。这样的事情。

public class SomeLists {

    public static void main(String[] args) {
        MyList l1 = new MyList();
        ListNode listNode1 = new ListNode(1);
        l1.add(listNode1);
        ListNode listNode2 = new ListNode(2);
        l1.add(listNode2);

        MyList l2 = new MyList();
        ListNode listNode3 = new ListNode(3);
        l2.add(listNode3);
        ListNode listNode4 = new ListNode(4);
        l2.add(listNode4);

        MyList sum = SomeLists.Sum2List(l1, l2);
        ListNode first = sum.getFirst();
        ListNode second = first.getNext();
        assert (first.getData() == 3);
        assert (second.getData() == 6);
    }

    public static MyList Sum2List(MyList L1, MyList L2) {
        MyList sum = new MyList();

        ListNode currentNode1 = L1.getFirst();
        ListNode currentNode2 = L2.getFirst();
        while (currentNode1 != null && currentNode2 != null) {
            sum.add(new ListNode(currentNode1.getData() + currentNode2.getData()));
            currentNode1 = currentNode1.getNext();
            currentNode2 = currentNode2.getNext();
        }

        return sum;
    }

    private static class MyList {
        private ListNode first;

        public void add(ListNode listNode) {
            if (first == null) {
                this.first = listNode;
            }
            else {
                first.addLast(listNode);
            }
        }

        public ListNode getFirst() {
            return first;
        }
    }

    private static class ListNode {
        private ListNode next;
        private int data;

        public ListNode(int i) {
            this.data = i;
        }

        public void addLast(ListNode listNode) {
            if (this.next == null) {
                this.next = listNode;
            }
            else {
                this.next.addLast(listNode);
            }
        }

        public ListNode getNext() {
            return next;
        }

        public int getData() {
            return data;
        }
    }
}

然后,为什么要实施轮子? Java中已有一个列表

import java.util.ArrayList;
import java.util.List;

public class SomeLists {

    public static void main(String[] args) {
        List<Integer> l1 = new ArrayList<Integer>();
        List<Integer> l2 = new ArrayList<Integer>();

        l1.add(1);
        l1.add(2);

        l2.add(3);
        l2.add(4);

        List<Integer> sum = SomeLists.Sum2List(l1, l2);
        assert (sum.get(0) == 3);
        assert (sum.get(1) == 6);
    }

    public static List<Integer> Sum2List(List<Integer> l1, List<Integer> l2) {
        List<Integer> sum = new ArrayList<Integer>();

        for (int i = 0; i < l1.size() && i < l2.size(); i++) {
            sum.add(l1.get(i) + l2.get(i));
        }

        return sum;
    }
}

答案 1 :(得分:0)

这是一种方法。不是最好的。但是一种方式:

public static list Sum2List(list L1, list L2) {
    list l=null;
    int listCounter = 0;
    for (listNode p = L1.first; p != null; p = p.next) {
        for (listNode p1 = L2.first; p1 != null; p1 = p1.next) {
            l.set(listCounter++, p.data + p1.data);
        }
    }
    return l;
}

在方法列表中,添加一个方法。假如你的底层数据结构是一个ArrayList,那么你可以这样做:

public void set(int index, int value) {
    this.arrayList.set(index,value);
}

答案 2 :(得分:0)

this is the solution for problem

  public static list Sum2List(list L1, list L2) {
        list l = new list();
        l.first = new listNode(0, null);
        listNode x = l.first;
        for (listNode p = L1.first, p1 = L2.first; (p != null && p1 != null); p = p.next, p1 = p1.next) {
            if (p.next != null && p1.next != null) {
                x.next = new listNode(p.data + p1.data, x.next);
            } else {
                x.data = p.data + p1.data;
            }
        }
        return l;
    }