我学习Java并用数组构建程序:

com[0]="one";
com[1]="two";
com[2]="three";
[...]
com[9]="ten";


数组的每个字符串都是一条诫命(我的程序是“十诫”)。

我想检查一条诫命是否已经阅读。因此,我认为使用具有字符串数组和布尔数组的多维数组。

有可能吗?做这个的最好方式是什么?

谢谢!

最佳答案

这里不需要多维数组,这只会增加复杂性。您只需要一个类Commandment:

public class Commandment {

   private String commandment;
   private boolean read;

   public Commandment(String commandment) {
      this.commandment = commandment;
   }

   public void setRead(boolean read) {
      this.read = read;
   }

   public boolean isRead() {
      return this.read;
   }
}


然后,创建一个数组数组:

com[0]= new Commandment("one");
com[1]= new Commandment("two");
com[2]= new Commandment("three");


更改为“读取”:

com[2].setRead(true);

07-26 05:59