For a project that i am doing, i would like to use a separate class to store information of various people in an arraylist. in this case, the method will contain an arraylist of strings to store all my information. when i tried doing this i realized that every time i run the storage
to add a string to the arraylist, it gets rid of all previous info from the arraylist.
is it possible to make it so both the Strings Hello, How Are You?
and I'm fine. How Are You?
add to the array in class two
without having the array reset once the method is rerun?
public class one
{
public static void main (String [] args)
{
two t = new two();
t.storage("Hello, How Are You?");
t.storage("I'm fine. How Are You?");
}
}
import java.util.ArrayList;
public class two
{
public static void storage(String toBeAdded)
{
ArrayList<String> al = new ArrayList<String>();
al.add(toBeAdded);
System.out.println(al);
}
}
Given Output:
[Hello, How Are You?]
[I'm fine. How Are You?]
Expected Output:
[Hello, How Are You?]
[Hello, How Are You?,
I'm fine. How Are You?]