[英]How to use an ArrayList from one class in another?
我對Java相當陌生,這聽起來有些奇怪。 好的,基本上我要接受9個值並將它們作為整數存儲在ArrayListScores中。
我已經證實它們存儲在其中,並且一切看起來都還可以。
我正在開發一個簡單的androids應用程序。
因此,我們就好像我從文本視圖parseInt中獲取類1中的9個值一樣。 這很好,這不是我的問題。
1類
ArrayList<Integer> Scores = new ArrayList<Integer>();
Scores.add(Integer.parseInt(et1.getText().toString()));
Scores.add(Integer.parseInt(et2.getText().toString()));
Scores.add(Integer.parseInt(et3.getText().toString()));
Scores.add(Integer.parseInt(et4.getText().toString()));
Scores.add(Integer.parseInt(et5.getText().toString()));
Scores.add(Integer.parseInt(et6.getText().toString()));
Scores.add(Integer.parseInt(et7.getText().toString()));
Scores.add(Integer.parseInt(et8.getText().toString()));
Scores.add(Integer.parseInt(et9.getText().toString()));
我的問題是,我還有另一堂課,我想做一些基本的計算,只需將所有分數加起來即可。
2級
public class AddNumbers {
private static AddNumbers instance;
private AddNumbers(){
}
public static AddNumbers getInstance() {
if(instance == null) {
instance = new AddNumbers();
}
return instance;
}
public int getFinalScore() {
ArrayList<Integer> Scores = new ArrayList<Integer>();
int final_score = 0;
for(int s: Scores){
final_score += s;
}
return final_score;
}
}
我要對第2類的所有分數進行基本累加,然后將結果發送回第1類,但我不知道如何做。
您真的需要其他課程嗎? 為什么不將其放在Class 1的方法中?
它看起來像:
public int getFinalScore(){
您要在此處放入ArrayList
。 看起來應該像這樣:
public int getFinalScore(ArrayList<Integer> Scores) {
然后放入for
循環,並返回final_score
:
int final_score = 0;
for (int s: Scores) {
final_score += s;
}
return final_score;
因此,您的最終方法將如下所示:
public int getFinalScore(ArrayList<Integer> Scores) {
int final_score = 0;
for (int s: Scores) {
final_score += s;
}
return final_score;
}
您可以通過getFinalScore(Scores)
調用它。
通過從類1成績作為參數成2級的getFinalScore方法
public int getFinalScore(List<Score> scores) {
int final_score = 0;
for(int s: scores){
final_score += s;
}
return final_score;
}
然后使用返回值在1級的總和。
我要做的是從類1中創建一個ArrayList的變量/實例。因此,首先您需要公開Scores,以便您的其他類可以訪問它:
public static ArrayList<Integer> Scores = new ArrayList<Integer>(); //add public and static
Scores.add(Integer.parseInt(et1.getText().toString()));
Scores.add(Integer.parseInt(et2.getText().toString()));
Scores.add(Integer.parseInt(et3.getText().toString()));
Scores.add(Integer.parseInt(et4.getText().toString()));
Scores.add(Integer.parseInt(et5.getText().toString()));
Scores.add(Integer.parseInt(et6.getText().toString()));
Scores.add(Integer.parseInt(et7.getText().toString()));
Scores.add(Integer.parseInt(et8.getText().toString()));
Scores.add(Integer.parseInt(et9.getText().toString()));
然后,您需要像這樣參考它:
public int getFinalScore() {
ArrayList<Integer> scores = Class1.Scores; //make a new variable referring to the Scores (Case Matters!)
int final_score = 0;
for(int s: scores){ //use the new variable
final_score += s;
}
return final_score;
}
另外,如果您想再次在另一個類中使用它,則可以將新的變量scores
(對於CASE而言是重要的)設為公共和靜態(如果需要的話,則沒有必要)。 但是,您仍然需要將ArrayList公開! 公開分數將如下所示:
public class AddNumbers {
private static AddNumbers instance;
public static ArrayList<Integer> scores = Class1.Scores //made the variable public and static (optional)
private AddNumbers(){
}
public static AddNumbers getInstance() {
if(instance == null) {
instance = new AddNumbers();
}
return instance;
}
//same as before
public int getFinalScore() {
ArrayList<Integer> scores = Class1.Scores; //make a new variable referring to the Scores (Case Matters!)
int final_score = 0;
for(int s: scores){ //use the new variable
final_score += s;
}
return final_score;
}
}
同樣,您可以創建一個新參數並將其設置為“分數”(同樣,您仍然需要公開分數):
public int getFinalScore(ArrayList<Integer> scores) {
scores = Class1.Scores //set local variable to public variable
int final_score = 0;
for(int s: scores){
final_score += s;
}
return final_score;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.