0

私はJavaを研究し、配列を使用してプログラムを作成します。

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

配列のすべての文字列は戒めです(私のプログラムは十戒です)。

戒めがすでに読まれているか確認したいのですが。したがって、文字列配列とブール配列を含む多次元配列を使用すると思います。

それは可能ですか?これを行うための最良の方法は何ですか?

ありがとう!

4

3 に答える 3

3

ここでは多次元配列は必要ありません。これは複雑さを増すだけです。あなたはただクラスの戒めが必要です:

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);
于 2012-12-27T11:56:57.827 に答える
1

別の配列、同じ長さ、および文字列配列内のインデックスに関連するインデックスを用意します。

おそらくより良い方法は、次のようなオブジェクトを作成することです

public class Commandment {
    private String com;
    private String read;
    public (String com) {
        this.com = com;
        this.read = false;
    }
    public getCom() {
        return com;
    }
    public isRead() {
        return read;
    }
    public beenRead() {
        read = true;
    }
}

代わりに、それらのオブジェクトの配列を作成します。

Commandment[] coms = new Commandment[10];
coms[0] = new Commandment("com1");
System.out.println(coms[0].getCom()+", has been read? "+coms[0].isRead());
coms[0].beenRead();
System.out.println(coms[0].getCom()+", has been read? "+coms[0].isRead());

それを作成し、最初の戒めを「com1」として入力し、それが読み取られたかどうかを確認し、読み取らせてから、もう一度確認します。

于 2012-12-27T11:56:34.050 に答える
1

または、2つのコレクションを使用できます

String[] commandments="zero,one,two,three,four,five,six,seven,eight,nine,ten".split(",");
BitSet read = new BitSet(commandments.length);
于 2012-12-27T12:12:17.950 に答える