在eclipse中将这两个类放在一个.java文件中显示找不到主类,运行不了程序。将这两个类分别建立一个.java文件就可以运行
class TestMain{
public static void main(String[] args) {
System.out.println("jiasuo");
ArrayStack as=new ArrayStack(4);
as.push(3);
as.push(5);
as.push(23);
as.push(433);
as.each();
}
}
public class ArrayStack {
private int top;
private int maxsize;
private int []AStack;
private int length=0;//表示静态栈的长度
public ArrayStack(int maxsize) {
this.maxsize=maxsize;
top=-1;
AStack=new int[maxsize];
}
//判断静态栈是否为空
public boolean isEmpty() {
return top==-1;
}
//判断静态栈是否满了
public boolean isFull() {
return top==maxsize-1;
}
//压栈
public void push(int a) {
if(isFull()) {
throw new RuntimeException("栈满");
}
top++;
AStack[top]=a;
length++;
}
//弹栈
public int pop() {
if(isEmpty()){
throw new RuntimeException("栈空");
}
int value=AStack[top];
top--;
length--;
return value;
}
//遍历元素
public void each() {
if(isEmpty()) {
throw new RuntimeException("栈是空的,无法遍历");
}
for(int a:AStack) {
System.out.println(a);
}
}
}
您好,引发这种问题的原因可能是环境变量配置不当,也可能是编译问题。可以参考:https://blog.csdn.net/weixin_44462773/article/details/113919790
希望可以解决您的问题。
一个java文件中可以定义多个类,但是最多只有一个类被public修饰,并且这个类的类名与文件名必须相同,若这个文件中没有public的类,则文件名随便是一个类的名字即可。需要注意的是,当用javac指令编译有多个类的Java文件时,它会给每一个类生成一个对应的.class 文件;
所以的你代码只要把ArrayStack 类的public删除即可,代码如下
package cn.bdqn.demo02;
public class TestMain {
public static void main(String[] args) {
System.out.println("jiasuo");
ArrayStack as = new ArrayStack(4);
as.push(3);
as.push(5);
as.push(23);
as.push(433);
as.each();
}
}
class ArrayStack {
private int top;
private int maxsize;
private int[] AStack;
private int length = 0;// 表示静态栈的长度
public ArrayStack(int maxsize) {
this.maxsize = maxsize;
top = -1;
AStack = new int[maxsize];
}
// 判断静态栈是否为空
public boolean isEmpty() {
return top == -1;
}
// 判断静态栈是否满了
public boolean isFull() {
return top == maxsize - 1;
}
// 压栈
public void push(int a) {
if (isFull()) {
throw new RuntimeException("栈满");
}
top++;
AStack[top] = a;
length++;
}
// 弹栈
public int pop() {
if (isEmpty()) {
throw new RuntimeException("栈空");
}
int value = AStack[top];
top--;
length--;
return value;
}
// 遍历元素
public void each() {
if (isEmpty()) {
throw new RuntimeException("栈是空的,无法遍历");
}
for (int a : AStack) {
System.out.println(a);
}
}
}