问题描述
在添加单链列表的元素时,我遇到了运行时错误。我已经enter image description here安装了错误图片。我在这里附上我的代码:
public class Node{
Node next;
int a=0;
public Node (int e,Node n){
a =e;
next =n;
}}
public class Question6{
static int sum=0;
public static void main(String[]args){
Node head=null;
Node n5= new Node(5,null);
Node n4= new Node(4,n5);
Node n3= new Node(3,n4);
Node n2= new Node(2,n3);
Node n1= new Node(1,n2);
head=n1;
sum =add(head);
}
public static int add(Node head){
if(head==null){
return sum;
}
else{
sum=sum + head.a;
head=head.next;
add(head);
}
return sum;
}
}
解决方法
您的编译器似乎有问题。图像中的错误表明它无法在类Node
中找到init方法。
尝试在类Node
中添加空的init方法(花括号):
class Node{
Node next;
int a = 0;
{}
public Node(int e,Node n) {
a = e;
next = n;
}
}
,
首先,由于您使用class SizeWidget extends StatefulWidget {
@override
_SizeWidgetState createState() => _SizeWidgetState();
}
class _SizeWidgetState extends State<SizeWidget> {
final GlobalKey _textKey = GlobalKey();
Size textSize;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => getSizeAndPosition());
}
getSizeAndPosition() {
RenderBox _cardBox = _textKey.currentContext.findRenderObject();
textSize = _cardBox.size;
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Size Position"),),body: Column(
mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.stretch,children: <Widget>[
Text(
"Currern Size of Text",key: _textKey,textAlign: TextAlign.center,style: TextStyle(fontSize: 22,fontWeight: FontWeight.bold),SizedBox(
height: 20,Text(
"Size - $textSize",],);
}
}
,因此您的代码不是real
递归。
external static variable