简体   繁体   中英

How to use an ArrayList from one class in another?

I'm fairly new to Java and this may sound a bit strange. Okay basically I'm taking in 9 values and storing them as Integers in an ArrayListScores.

I have verified that they are storing in there and all looks okay with that.

I'm developing a simple androids app.

So we take it as if I take in the 9 values in class 1 as such from text views parseInt them. This works fine that is not my issue.

Class 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()));

My Problem is that I have another class which I want to do some basic calculations, just add up all the scores as such.

Class 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;
    }
}

I was to do the basic adding up of all the scores in class 2 and send the result back to class 1 but I don't know how.

Do you really need another class for this? Why not just put this in a method in Class 1?

It would look like:

public int getFinalScore(){

You want to put in your ArrayList here. This should look like:

public int getFinalScore(ArrayList<Integer> Scores) {

Then put your for loop, and return final_score :

int final_score = 0;

for (int s: Scores) {
    final_score += s;
}

return final_score;

So your final method would look like this:

public int getFinalScore(ArrayList<Integer> Scores) {
    int final_score = 0;
    for (int s: Scores) {
        final_score += s;
    }

    return final_score;
}

You would call it just via getFinalScore(Scores) .

Pass Scores from Class 1 as a parameter into the getFinalScore method in Class 2

public int getFinalScore(List<Score> scores) {
    int final_score = 0;

    for(int s: scores){
        final_score += s;
    }

    return final_score;
}

Then use the return value as your sum in Class 1.

What I would do is make a variable/instance of the ArrayList from class 1. so first you need to make Scores public so your other class can access it:

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()));

Then, you need to refer back to it like so:

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;
}

Alternatively, you can make the new variable scores (CASE matters) public and static if you want to use it in yet another class again (if you want to, this isn't necessary). However, you still need to make the ArrayList public! The public scores would look like this:

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;
    }
}

Alternatively again, you can make a new parameter and set it to Scores (Again, you still need to make Scores public):

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM