How to use a method in java to store information from an arraylist
For a project I am doing, I would like to use a separate class to store various people's information in an arraylist. in this case, the method will contain an arraylist of strings to hold all my information. when i tried to do this i realized that every time i run storage
to add a line to the arraylist it gets rid of all the previous information from the arraylist.
is it possible to make strings Hello, How Are You?
u I'm fine. How Are You?
add to array in two
non-array reset class after method re-run?
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);
}
}
This conclusion:
[Hello, How Are You?]
[I'm fine. How Are You?]
Expected Result:
[Hello, How Are You?]
[Hello, How Are You?,
I'm fine. How Are You?]
source to share
There are two options for solving your problem:
Option (1): The current scope is local to the method , since you are creating a brand for each call (to ), but you need an object at the class level as shown below, but since you are calling the method using an object, this is not the preferred option (explained below ) is clear) and the compiler is already throwing a warning and you ignore it. ArrayList
storage
new
ArrayList
storage()
static
ArrayList
storage()
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 scope static
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 that 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 make sure that variables / methods are called using the class name (e.g. Two.storage ()) without creating any object , because they are members of the class, i.e. they are not meant for a single object. I highly recommend you read this and understand this topic more clearly. static
Apart from the important point, always make sure you follow Java naming standards for example class names must start with uppercase which you are breaking.
source to share
Your mistake
Each time you call the method storage()
, you create a new ArrayList object.
Decision
So, create a class object two
and pass it along with a string to the methodstorage()
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);
}
}
source to share
in your class the two
problem is in the method storage
your logic is incorrect every time you call the store to store a new row you create a new arraylist al
which will remove all the previous information from the old arraylist.
to decide what to define an arraylist static
in the second class and add information to it using storage methods:
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 the method storage
shouldn't be static
, because you create a class object two
and call the method through this object, so if you try to test it will warn you:
access to storage of static methods
the reason for the warning that you are trying to access a static method storage
in your t
class object two
.
when you declare a static method on a class in the correct way to call it:
ClassName.MethodName()
in your example:
two.storage("Hello, How Are You?");
two.storage("I'm fine. How Are You?");
source to share