我想为大小为 x 的数组编写一个构造函数,其中 x 是 main() 中指定的参数。
我的类(class):
public class CharA
{
private char[] stack;
private int n = 0;
public void CharA (int max)
{
this.stack = new char[max];
this.n = max;
}
我的主要():
public class CharTest
{
public static void main (String args)
{
CharA stack1 = new CharA(100);
}
}
错误:
CharTest.java:5: cannot find symbol
symbol : constructor CharA(int)
location: class CharA
CharA stack1 = new CharA(100);
^
这里有几个例子,其中使用 int 数组完成相同的事情。为什么它不适用于这个 char 数组?
最佳答案
删除“构造函数”中的void
:
public CharA (int max) {
// ...
}
关于java - 可变大小数组的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8918660/