Reputation: 41
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?]
Upvotes: 3
Views: 1876
Reputation: 2351
in your class two
the problem in the method storage
your logic isn't right every time you call storage to save new string you create new arraylist al
which will erase all previous information in the old arraylist.
to solve that define static
arraylist in class two and add information to it by method storage:
public class two
{
public static ArrayList<String> al = new ArrayList<String>();
public void storage(String toBeAdded)
{
al.add(toBeAdded);
System.out.println(al);
}
}
Note : also storage
method shouldn't be static
method because you are creating object of class two
and calling the method through this object so if you try to test it it will give you warning:
access static method storage
reason of warning that you are trying to access static method storage
in your object t
of class two
.
when you declare static method in class the right way to call it:
ClassName.MethodName()
in your example:
two.storage("Hello, How Are You?");
two.storage("I'm fine. How Are You?");
Upvotes: 0
Reputation: 22452
There are two options to solve your problem:
Option(1): Your current ArrayList
scope is local to the storage
method because you are creating a brand new
ArrayList
on each call (to storage()
method), but what you need is a static
ArrayList
object at class level as shown below, but because you are invoking storage()
method using an object, this is not preferable option (explained below clearly) and compiler is already throwing a warning and you are ignoring it.
public class two {
private static ArrayList<String> al = new ArrayList<String>();
public static void storage(String toBeAdded)
{
al.add(toBeAdded);
System.out.println(al);
}
}
Option(2) (Prefer this): Remove the static
scope and declare ArrayList<String>
as an instance variable as shown below (prefer this option) because you are calling a static
method using an object reference which is not required and creates confusion.
public class two {
private ArrayList<String> al = new ArrayList<String>();
public void storage(String toBeAdded)
{
al.add(toBeAdded);
System.out.println(al);
}
}
Always ensure that static
variables/methods are invoked using classname (like Two.storage()) without creating any object because they are class level members i.e., they are not meant for a single object. I strongly suggest you read this and understand this subject more clearly.
Apart from above important point, always ensure that you follow the Java naming standards like class names should start with uppercase which you are violating.
Upvotes: 2
Reputation: 2011
Your mistake
Each time you are calling storage()
method,you are creating a new object of 'ArrayList'.
Solution
So,make an object of class two
and pass it alongwith the string to the method storage()
import java.util.ArrayList;
public class one
{
public static void main (String [] args)
{
two t = new two();
t.storage(t,"Hello, How Are You?");
t.storage(t,"I'm fine. How Are You?");
}
}
class two
{
ArrayList<String> al = new ArrayList<String>();
public static void storage(two object,String toBeAdded)
{
object.al.add(toBeAdded);
System.out.println(object.al);
}
}
Upvotes: 1
Reputation: 54742
Instead of declaring the ArrayList as local variable, use it as a field. Also make the method non-static
public class two
{
private ArrayList<String> al = new ArrayList<String>();
public void storage(String toBeAdded)
{
al.add(toBeAdded);
System.out.println(al);
}
}
Upvotes: 2