import java.util.*;
class BallList{
private LinkedList<Integer> list = new LinkedList<Integer>();
public BallList(int n){
for (int i = 0; i < n; i++){
list.add(i+1);
}
}
public void DoAxy(int num1, int num2){
Iterator iterator = list.iterator();
int count = 0;
while (iterator.hasNext()){
count++;
if (iterator.next().equals(num1)){
break;
}
}
int index = count;
list.remove(num2);
list.add(index,num2);
}
public void DoBxy(int num1, int num2){
Iterator iterator = list.iterator();
int count = 0;
while (iterator.hasNext()){
count++;
if (iterator.next().equals(num1)){
break;
}
}
int index = count-1 ;
list.remove(num2);
list.add(index,num2);
}
public void DoRemove(int num){
list.remove(num);
}
public LinkedList <Integer> getList(){
return list;
}
}
class Balls2{
public static void main(String[] args){
int num_balls, num_ops;
String op;
Scanner sc = new Scanner(System.in);
num_balls = sc.nextInt();
num_ops = sc.nextInt();
BallList Arrayballs = new BallList(num_balls);
for (int i = 0; i < num_ops; i++){
op = sc.next();
if (op.equals("A")){
int ball_1 = sc.nextInt();
int ball_2 = sc.nextInt();
Arrayballs.DoAxy(ball_1, ball_2);
}
else if (op.equals("B")){
int ball_1 = sc.nextInt();
int ball_2 = sc.nextInt();
Arrayballs.DoBxy(ball_1, ball_2);
}
else{
int ball = sc.nextInt();
Arrayballs.DoRemove(ball);
}
}
}
LinkedList<Integer> listballs = Arrayballs.getList();
}
当我运行上面的代码时,我收到错误:
Balls2.java:80: error: cannot find symbol
LinkedList<Integer> listballs = Arrayballs.getList();
^
symbol: variable Arrayballs
location: class Balls2
1 error
有人可以帮我吗?
答案 0 :(得分:2)
此处的缩进没有帮助,但您正在执行
行LinkedList<Integer> listballs = Arrayballs.getList();
在main
方法之外;由于Arrayballs是该方法的本地变量,因此无法从外部访问它。
答案 1 :(得分:1)
您正在使用main方法调用Arrayballs.getList()
。它应该在里面请检查大括号
答案 2 :(得分:1)
您已在Arrayballs
方法中实例化了main
,并且您正试图从外部访问它。