[英]Understanding java code with arrays
我在android studio中創建了一個數組。 以下代碼是什么意思?
private question[] questionbank = new question[]{
new question(R.string.question_africa, true),
new question(R.string.question_oceans, false),
new question(R.string.question_mideast, true),
new question(R.string.question_america, false),
new question(R.string.question_asia, true);
我從一個名為問題的類中初始化了一個數組,如下所示
public class question {
private int TextInput;
private boolean AnswerInput;
private boolean hasCheated;
public boolean isHasCheated() {
return hasCheated;
}
public void setHasCheated(boolean hasCheated) {
this.hasCheated = hasCheated;
}
public question(int holdsTextInput, boolean holdsAnswerInput) {
this. TextInput = holdsTextInput;
this.AnswerInput = holdsAnswerInput;
}
public int getTextInput() {
return TextInput;
}
public boolean getAnswerInput() {
return AnswerInput;
}
public void setTextInput(int holdsTextInput) {
this.TextInput = holdsTextInput;
}
public void setsAnswerInput(boolean holdsAnswer) {
this.AnswerInput = holdsAnswer;
}
}
但是,我想澄清一下,例如,這里到底做了什么? 我是否引用了類問題並創建了一個名為Questionbank的數組? 同樣如果是這樣,為什么我需要將每個值都作為一個新問題?
我是否引用了類問題並創建了一個名為Questionbank的數組?
您創建了一個只能存儲question
類型為object的數組。 該數組本身是一個對象,並且對該數組的引用已分配給名為questionbank
的字段。
請注意,數組沒有名稱,只是從具有該名稱的字段中引用。 您可以將相同的引用分配給其他名稱的字段和變量,並且仍然是相同的數組。
同樣如果是這樣,為什么我需要將每個值都作為一個新問題?
在Java中,對象數組實際上是對象引用的數組,因此在創建數組時,實際上並沒有創建任何對象。
這就是為什么您必須創建5個question
實例並將其引用分配給數組的原因。
如果不這樣做,則將有一個null
值數組。
首先,我們將創建一個名為questionbank
對象,其類型為Array
,其中包含question
類型的元素。
步驟1:通過將引用變量聲明為波紋管:
private question[] questionbank.....;
questionbank
擁有類型為Array
的單個對象的引用。 通過這一部分, 沒有對象被創建。
步驟2:通過編寫波紋管部分,我們將創建一個新的Array
類型的單個對象,該元素類型是有問題的
new question[]{......};
步驟3:最后,我們通過在上面放一些question
對象來初始化我們新創建的單個數組對象:
{
new question(R.string.question_africa, true),
new question(R.string.question_oceans, false),
new question(R.string.question_mideast, true),
new question(R.string.question_america, false),
new question(R.string.question_asia, true)..}
這里每次都使用new question(..)
因為在將question
對象放入questionbank
數組對象之前,我們需要使用new
關鍵字創建它們。
我是否引用了類問題並創建了一個名為Questionbank的數組?
question[] questionbank = new question[];
這意味着創建一個名為questionbank
的數組,該數組將僅包含question
對象。 該名稱用於稍后引用數組:
question q = questionbank[0]; // get the first element from the array
另一個示例:
String[] arr = new String[5];
在這里,您創建了一個名為arr
的數組,該數組僅包含5個字符串。
如果是這樣,為什么我需要將每個值都作為一個新問題?
questionbank
數組只能容納question
對象。 因此,使用new question(...)
添加對象是正確的方法。
是的,您創建了一個名為問題庫的數組。
我認為您對類的概念感到困惑。 “ new”關鍵字只是調用問題類的構造函數的一種好方法:
public question(int holdsTextInput, boolean holdsAnswerInput)
每次調用此構造函數時,都會創建一個新的問題實例 。 例如,代碼行:
new question(R.string.question_africa, true)
創建問題類的實例 ,其TextInput字段設置為R.string.question_africa,AnswerInput字段設置為true。
另一方面,該行:
new question(R.string.question_oceans, false)
創建TextInput設置為R.string.question_oceans且AnswerInput設置為false的問題類的實例 。
因為兩次都使用了“ new”關鍵字,所以這兩個問題實例之間沒有關系。 改變一個不會改變另一個。
您不必每次都打新電話。 例如,您可以這樣做:
question q = new question(R.string.question_africa, true);
private question[] questionbank = new question[]{q, q, q, q, q};
但是正如您所看到的,存儲在數組中的每個問題實際上都引用相同的對象(q),因此它們將具有相同的字段(TextInput,AnswerInput),並且更改數組的一個元素將更改數組的所有其他元素(因為它們都指向相同的問題實例 。
首先嘗試對類名使用正確的大小寫。
Question[] question = new Question[size];
然后,根據需要使用構造函數或方法。
構造函數訪問:
question[0] = new Question(x,y,z);
方法訪問:
question[0] = new Question();
.
.
question[size] = new Question();
question[0].isHasCheated();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.