java 构造函数初始化数组,如何在类的构造函数中初始化数组

在Java中,不能直接在类的构造函数中赋值给数组,但可以通过构造函数内部调用strcpy等方法来实现初始化。推荐使用std::string代替字符数组,或者在类的初始化列表中设置值。另外,避免使用固定大小的数组,可以考虑使用STL的std::string作为文本容器。
摘要由CSDN通过智能技术生成

Please solve my problem.

class Book

{

public :

Book() { } // How should i intialize both the array in constructor.

private :

char itsTitle[2][20];

char itsAuthor[2][20];

};

I know the initialization of the above array itsTitle[2][20] can be done as follows :

char itsTitle[2][20]={"C++ Programming","Java Programming"};

But in class we cannot assign we can only declare. Assignment can be done only through member function of that class or constructor of that class. And my question is how to initialize the array itsTitle[2][20] & itsAuthor[2][20] in "constructor of class Book".

解决方案

Well, you could use strcpy[^] in the constructor, can''t you?

I''d also second the recommendation of using an std::string instead of a characte

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值