D Rodriguez
D Rodriguez

Reputation: 41

Call Array List Method

I wanted to use a method that can be accessed by the main, that will store the array list. But I want the declaration of the array list to be in the main not the store method.

Out side this method ill be using simple JOptionPanes to display the array list in the main.

Example here.

public static void main(String[] arg) throws Exception
{
 ArrayList<String> Logo = new ArrayList<String>();

    Initials();

    public String Initials()
    {
        Logo.add("Blah blah");
        Logo.add("Blah blah");
    }

    JOptionPane.showMessageDialog(null, Logo);
}

Please help me I'm not sure how to due this. I want to store it because it take a lot of the space and is kinda a eye sore. Ill be storeing a lot in the array list.

Or a different better way of doing this

Upvotes: 0

Views: 72

Answers (2)

John Kugelman
John Kugelman

Reputation: 361625

Methods don't nest. The helper method would go before or after main. You can do this by passing the list to the method.

public static void main(String[] arg) throws Exception
{
    ArrayList<String> logo = new ArrayList<String>();

    initialize(logo);

    JOptionPane.showMessageDialog(null, logo);
}

private void initialize(ArrayList<String> logo)
{
    logo.add("Blah blah");
    logo.add("Blah blah");
}

Upvotes: 3

GeorgeDopeG
GeorgeDopeG

Reputation: 175

You can have the arraylist in the main, but also you can create method outside of main and reference that arraylist to it like this:

public static void main(String[] arg) throws Exception
{
ArrayList<String> Logo = new ArrayList<String>();

Initials(Logo);


}
public static String Initials(ArrayList <String> Logo)
{
    Logo.add("Blah blah");
    Logo.add("Blah blah");
    return ("blah");
}

Upvotes: 1

Related Questions