Javaには新しいので、オブジェクト配列の仕組みを理解したい。このようなシナリオでは、コールはキュー[0-9]に配置され、これらのケースをモデル化しようとしています。キュー内の呼び出しのJava配列とオブジェクト
キューを形成するコールの配列であるインスタンス変数。 2番目のインスタンス変数で、現在キューにある呼び出しの数を保持するint型です。 キューに必要なサイズをパラメータとし、配列をそのサイズに初期化するコンストラクタです。 void add(呼び出し呼び出し)メソッドは、呼び出しをキューに追加します。 現在キューにあるコールの数を取得する方法。 キューが現在空であるかどうかを調べるメソッドboolean isEmpty()。 キューが満杯かどうかを確認する新しい方法。
public class TrafficQueue {
private Call[] callArray;
private int numberOfCalls;
private int addIndex=-1;
public TrafficQueue (int size)
{
callArray = new Call[size];
}
public void add(Call call)
{
addIndex++;
callArray[addIndex] = call;
}
//can't return an int here just true/false.
public boolean isEmpty()
{
boolean isEmpty= false;
for (int i = 0; i < callArray.length; i++){
if(callArray[i] != null)
{
isEmpty = true;
}
}
return isEmpty;
}
public int NumberofCalls()
{
return addIndex + 1;
}
//We can't simply check the count, we have to make sure that at each index there is a value.
//Count just tells us the size of the array.
public boolean isfull()
{
int notNull = -1;
for(int i=0; i<callArray.length; i++){
if(callArray[i] != null)
{
notNull++;
}
}
if(notNull == callArray.length -1)
{
return true;
}
return false;
}
どのようにこれらのシナリオをモデル化できますか?このアプローチは正しいですか?そこには良い方法がありますか?
Qの最後に追加するだけで、isfull()およびisEmpty()メソッドを単純化して、配列の最後または最初の要素だけをチェックすることができます。また、私的なint numberOfCallsは使用されません –
ok私はこれについてどうやって行くのですか? – blueGOLD
callArray [0] == nullの場合、Qは空です。 callArray [callArray.length-1]!= null –