-3

Please read the post before marking it as duplicate.

and I am not a Java expert

This is a simplified code of what I am trying to do:

import java.util.ArrayList;

public class Parameter {

    private static ArrayList<Integer> quantityArray = new ArrayList<>();

    public static void quantityPriorityValues() {
        int q = 0 ;
        for (int i = 0; i < 5 ; i++ ) {
            quantityArray.add(q);
            q++;
        }
    }

    public static ArrayList<Integer> getQuantityArray() {
        return quantityArray;
    }

    public void setQuantityArray(ArrayList<Integer> quantityArray) {
         this.quantityArray = quantityArray;
    }
}

public class Connection {

    public void getParameters () {
        i = 0;    
     Parameter.quantityPriorityValues();

        while (i < 5) {

            int quantity = Parameter.getQuantityArray().get(i);
            System.out.println(quantity);
            i++;

        }
    }

}

It always prints out the last value of the arraylist. I checked answers about this problem , like this one and this one and they both said that the problem in in the staticfields.So I tried to remove the static from ArrayList<Integer> quantityArray but that means that the getters and setters should be non-static too and I won't be able to call the getter in the Connection class.

I want this output: 0 1 2 3 4

But I get this output: 4

Does anyone have another solution?

Community
  • 1
  • 1
AIR
  • 73
  • 1
  • 9
  • Can you please add the expected output and the one that you get? – StepTNT May 04 '16 at 09:17
  • 3
    Please include your main method, so we can see where you initialize the list. – Eran May 04 '16 at 09:19
  • 3
    Indeed - ideally, rewrite this as a [mcve]. – Jon Skeet May 04 '16 at 09:19
  • @StepTNT I edited my question – AIR May 04 '16 at 09:20
  • @Eran : In reality those two classes are JDialog classes and In my main method I just create a new instance of the class and set it as visible. – AIR May 04 '16 at 09:22
  • I added the output and the expected out so that you can see better my problem. – AIR May 04 '16 at 09:23
  • Please show your main-method. It seems that you overwrite your list somewhere – Supahupe May 04 '16 at 09:26
  • @Supahupe : I have nothing about the lists in the main method and as I said to Eran "In reality those two classes are JDialog classes and In my main method I just create a new instance of the class and set it as visible." – AIR May 04 '16 at 09:28
  • before retrieving the elements, just try to print the arraylist `System.out.println(quantityArray);`, that will help in debugging – piyushj May 04 '16 at 09:29
  • This is all nice, but now do, what Jon Skeet suggested. Thank you :). – Tom May 04 '16 at 09:29
  • @piyushjaiswal : yes I have already done that and the elements in the arrayList are perfectly correct. My problem is in the elements' retrieving. – AIR May 04 '16 at 09:31

1 Answers1

0

In your getParameters() function Create an ArrayList like :

ArrayList<Integer> quantityArray = Parameters.getQuantityArray();

You can change your while loop like :

for (Integer i : quantityArray)
    System.out.println(i);

It should work

Jikar
  • 48
  • 9