Hi all,

I'm having a bit of a problem with my code. I've created an arraylist method and when values are inputted it prints them one by one.

import java.util.*;

public class ArrayListDemo{


  public static void test(int i){

      List arrlist = new ArrayList();
      arrlist.add(i);
      System.out.print(arrlist);

     }
 
  public static void main(String[] args) {
    
      test(10);
      test(20);
      test(30);
      test(40);
      test(50);

    }

}

the array list prints as [10]
[20]
[30]
[40] and so on.

How I want it to print is like,
[10]
[10, 20]
[10, 20, 30] and so on.

I think it's something wrong with the basics , but i just cant figure it out. Any help will be much appreciated.
Thanx for taking the time to read. :)

Each time you call your method test you create a new ArrayList. Just declare and initialise it outside that method.

Comments
thanx, it helped

Wow, thank you ever so much. I just couldn't figure it out and it was so simple.
thanx again. :D

This question has already been answered. Start a new discussion instead.