gpt4 book ai didi

Java对2个列表求和并存储在列表中

转载 作者:行者123 更新时间:2023-12-01 13:05:27 25 4
gpt4 key购买 nike

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循环编写这些求和列表方法的代码?

最佳答案

这是您的代码,可以正常工作。这通常不是在 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,那么它应该是面向对象的,成员变量应该是私有(private)的,并通过 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;
}
}

关于Java对2个列表求和并存储在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23300471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com