링크 된 목록으로 표시된 두 개의 숫자를 합산하려고합니다. 정적 지정자 및 내부 클래스에 문제가 있습니다. 나는 캐리를 추적하기 위해 내부 클래스 SumWrapper
을 정의했다. 내 합계 메서드에서이 래퍼를 사용합니다. 먼저 정적 메서드로 선언했지만 Eclipse에서 다음과 같은 오류가 발생했습니다.main에서 비 정적 메서드를 호출 할 수 있습니까?
솔루션의 둘러싼 인스턴스는 액세스 할 수 없습니다. 할당을 Solution 유형의 둘러싸는 인스턴스 (예 : x.new A() , x는 Exo25V2의 인스턴스)로 정규화해야합니다.
나는 다음 정적 지정자를 제거하지만,
는 해결 방법이 있습니까 ... 홈페이지에서 내 메소드를 호출하는 것이 불가능하게?
public class Solution {
public class SumWrapper{
public int carry = 0;
public Node node;
public SumWrapper(int c, Node n){
carry = c;
node = n;
}
} // close sumWrapper class
public SumWrapper sum(Node node1, Node node2){
if (node1 == null && node2 == null){
SumWrapper result = new SumWrapper(0, null);
return result;
}
int current = node1.data + node2.data + sum(node1.next, node2.next).carry;
int carry = (current >= 10) ? 1 : 0;
current = current % 10;
Node sumResult = new Node(current);
sumResult.next = sum(node1.next, node2.next).node;
SumWrapper result = new SumWrapper(carry, sumResult);
return result;
} // close sumWrapper method
public int listSize(Node node){
int result = 0;
while (node != null){
result++;
node = node.next;
}
return result;
} // close listSize method
public Node sumLists(Node node1, Node node2){
int size1 = listSize(node1);
int size2 = listSize(node2);
int size = (size1 >= size2) ? (size1+1):(size2+1);
while (size1 < size){
Node head1 = new Node(0);
head1.next = node1;
node1 = head1;
size1++;
}
while (size2 < size){
Node head2 = new Node(0);
head2.next = node2;
node2 = head2;
size2++;
}
SumWrapper wrap = sum(node1, node2);
return wrap.node;
} // close sumLists method
public static void main(String[] args){
Node head1 = new Node(6);
head1.appendToTail(1);
head1.appendToTail(1);
head1.appendToTail(7);
Node head2 = new Node(2);
head2.appendToTail(9);
head2.appendToTail(5);
Node result = sumLists(head1, head2);
Node.printLinkedList(result);
} // close main method
}
이 문제가 있습니까? – Sashwat