我正在尝试编写这个 java 程序来添加、删除数组列表中的项目。这是我编码的:
import java.io.*;
import java.util.Scanner;
class abc
{
ArrayList<Int> nums = new ArrayList<Int>();
Scanner in = new Scanner(System.in);
int opt = 0;
public void addItem(int i) // add item to list
{
nums.add(i);
}
public void addItem(int i, int pos) // add item to specific position in list
{
nums.add(pos,i);
}
public void delItem(int pos) // delete item at specific position in list
{
nums.remove(pos);
}
public void delItem() // clear all items in list
{
nums.clear();
}
public void showItems()
{
for(int i = 0; i < nums.length ; i++)
{
System.out.println("nums[" + i + "] : " + nums[i]);
}
}
public void menu()
{
System.out.println("==MENU==");
System.out.println("1) Add an item");
System.out.println("2) Add an item to specific position");
System.out.println("3) Delete an item at specific position");
System.out.println("4) Clear all items in list");
System.out.println("5) Exit \n\n");
System.out.println("Choose an option");
opt = in.nextInt();
execute();
}
public void execute()
{
if(opt == 1)
{
System.out.println("Enter a value: ");
int a = in.nextInt();
addItem(a);
System.out.println("Item added");
}
else if(opt == 2)
{
System.out.println("Enter a value: ");
int a = in.nextInt();
System.out.println("Enter a position: ");
int b = in.nextInt();
addItem(a,b);
System.out.println("Item added");
}
else if(opt == 3)
{
System.out.println("Enter a position: ");
int a = in.nextInt();
delItem(a);
System.out.println("Item deleted");
}
else if(opt == 4)
{
delItem();
System.out.println("All Items deleted");
}
}
}
class pList
{
public void static main(String args[])
{
abc a = new abc();
while(true)
{
a.menu();
if(a.opt == 5)
{
break;
}
else if(a.opt > 5)
{
a.menu();
}
}
}
}
这是我收到的错误:
C:\Users\Dummy\Desktop\Java>javac pList.java
pList.java:87: error: <identifier> expected
public void static main(String args[])
^
pList.java:87: error: '(' expected
public void static main(String args[])
^
pList.java:87: error: invalid method declaration; return type required
public void static main(String args[])
^
3 errors
C:\Users\Dummy\Desktop\Java>
请让我知道如何解决这个问题以及我在哪里犯了错误。提前致谢