简体   繁体   中英

ArrayList getting reinitialized everytime the method is called

I have written a method (part of a program) which takes in two int values(code below).
One int value representing number of guys for whom java training is completed and another int value for guys for whom php training is completed.
I expect the arraylist to grow with every function call.
Example: First time I called the function with values (5,0). So the arrayList for java would be [5] and for php it would be [0] .
Next time I call the function with values (2,3). The arrayList for java should now be [5][2] and sum should be 7. The the arraylist for php should be [0][3] ans sum should be 3.
The problem with my code is that when I call the method for the second time, it wipes away the [5](value of first index from the first call) in the java arrayList and just takes the form of [2] and gives the sum 2(instead of the required 7). The arrayList is never growing. (same for the php arrayList)
I am sure I am doing something conceptually wrong here . Please help Somehow, the way I have coded it, every function call seems to make a new arrayList and not growing the arrayList obtained from the previous call.

public class TrainingCamp {

public static int trainedJavaGuys ;
public static int trainedPHPGuys ;


public void trainedTroopsInCamp(int java,int php){

    //System.out.println("********* Current Status of Training Camp ********* ");


    ArrayList<Integer> trainingListJava = new ArrayList<>();
    trainingListJava.add(java);
    //System.out.println("---JavaARRAYLIST----"+trainingListJava);
    trainedJavaGuys = sumList(trainingListJava);


    ArrayList<Integer> trainingListPHP = new ArrayList<>();
    trainingListPHP.add(php);
    trainedPHPGuys = sumList(trainingListPHP);
    //System.out.println("---phpARRAYLIST----"+trainingListPHP);

Calling it like this from another class:

TrainingCamp currentTrainingCamp = new TrainingCamp();
currentTrainingCamp.trainedTroopsInCamp(2, 0);

and next time the same two lines get executed with just the input params changed

The arraylist s are reinitialized each time you call trainedTroopsInCamp() because they are declared within it.

You should make the arraylist s member variables , so that they only get initialized once, in the class's constructor .

public class TrainingCamp {

    public static int trainedJavaGuys ;
    public static int trainedPHPGuys ;

    // Declare once
    ArrayList<Integer> trainingListJava; 
    ArrayList<Integer> trainingListPHP;

    public TrainingCamp() {
        // Initialize once
        trainingListJava = new ArrayList<>();
        trainingListPHP = new ArrayList<>();
    }

    public void trainedTroopsInCamp(int java,int php){
        // Use everywhere
        trainingListJava.add(java);
        trainedJavaGuys = sumList(trainingListJava);

        trainingListPHP.add(php);
        trainedPHPGuys = sumList(trainingListPHP);
    }
}

you're re-initializing the list references in the method call, so every time you call the method you're using a new (empty) list.

instead, try keeping the lists as member variables for your class, something like this:

class TrainingCamp {

    private final List<Integer> javaTrained = new LinkedList<>();
    private final List<Integer> phpTrained = new LinkedList<>();

        public void trainedTroopsInCamp(int java,int php){

            //System.out.println("********* Current Status of Training Camp ********* ");


            javaTrained.add(java);
            trainedJavaGuys = sumList(javaTrained);


            phpTrained.add(php);
            trainedPHPGuys = sumList(phpTrained);

        }
    ...
}

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