0

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

3
  • 1
    It's not very clear what you're trying to do. You can't declare a method inside a method. Why not just populate the list normally? Commented Feb 9, 2017 at 4:01
  • Because there are about 60 full lines of code that I populated the list in the main it was just a lot I want it easy for my professor to read. If their is a better way to do this but simple enough, Ill be for it. Commented Feb 9, 2017 at 4:05
  • Your problem isn't clear and the code doesn't compile. Consider creating an MCVE Commented Feb 9, 2017 at 5:57

2 Answers 2

3

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");
}
Sign up to request clarification or add additional context in comments.

Comments

1

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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.