可变大小数组的构造函数

Mar*_*arv 0 java arrays constructor

我想为大小为x的数组编写构造函数,其中x是main()中指定的参数.
我的课:

public class CharA
{
  private char[] stack;
  private int n = 0;

  public void CharA (int max)
  {
    this.stack = new char[max];
    this.n = max;
  }
Run Code Online (Sandbox Code Playgroud)

我的主要():

public class CharTest
{
  public static void main (String args)
  {
    CharA stack1 = new CharA(100);
  }
}
Run Code Online (Sandbox Code Playgroud)

错误:

CharTest.java:5: cannot find symbol
symbol  : constructor CharA(int)
location: class CharA
    CharA stack1 = new CharA(100);
                   ^
Run Code Online (Sandbox Code Playgroud)

这里有几个例子,使用int数组完成同样的事情.为什么它对这个char数组不起作用?

Luk*_*der 6

删除void你的"构造函数":

public CharA (int max) {
  // ...
}
Run Code Online (Sandbox Code Playgroud)

  • @Marv:碰巧最好的:) (2认同)