I just want to learn. I just want to learn. - 23 days ago 9
Java Question

Program continues to run after method is finished

I have made a simple program, with two classes. A main class, which is below:

public class main {

public static void main(String[] args) {

myattributes attributes = new myattributes();

attributes.myArrayOne();

}
}


And a second class which contains the method of which I am calling in the above class as seen here:

public static int myArrayOne() {

ArrayList<Integer> packOfCards = new ArrayList<Integer>();

Random rand = new Random();

for (int j = 0; j<5; j++)
{
pick = rand.nextInt(10);
packOfCards.add(pick);
}

return myArrayOne();

}


The method works fine, however it continues to run after completing my method resulting in this being seen in the output and I have no idea why:

enter image description here

Answer

Your myArrayOne method calls itself. That's the problem with infinite recursion.

public static int myArrayOne() {
        // that's the problem
        return myArrayOne();
}

That is probably what are you trying to accomplish:

 // void --> List<Integer>
 // static 
 public List<Integer>  myArrayOne() {
     ArrayList<Integer> packOfCards = new ArrayList<Integer>();
     Random rand = new Random();

     for (int j = 0; j<5; j++)
     {
         pick = rand.nextInt(10);
         packOfCards.add(pick);
     }

     // myArrayOne(); --> packOfCards
     return packOfCards;
}

public static void main(String[] args) {
      myattributes attributes = new myattributes();
      List<Integer> values = attributes.myArrayOne(); 
}