Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7180855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:26:09+00:00 2026-05-28T17:26:09+00:00

I would like to return an arraylist from a non-class method in java, i

  • 0

I would like to return an arraylist from a non-class method in java, i have searched around but found nothing so far.

{
     int d=e.length();
     ArrayList list2 = new ArrayList();
     for (int i=0;i<d;i++)
     {
         list2.add('_');
     }
     return list2;
}

Thanks alot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:26:10+00:00Added an answer on May 28, 2026 at 5:26 pm

    All you need to do is put the return type in the signature, and then return the list

    public class Example {
        // a static method
        public static List someStaticMethod(){
           // create and return a List
        }
    
        // this method is not static
        public List someMethod(){
           // create and return a List
        }
    
    }
    

    Note that because ArrayList extends List you can make the return type either one of those.

    List is more generic, you can return any type of list. This is useful because if you want to change the internal list that you use (say change from ArrayList to LinkedList) you won’t have to change your API everywhere you use the method.

    However, if you use a list implementation that has its own methods that are not defined by the List interface, you won’t be able to access those methods when you get the list back from your method, unless you either cast (ewe) or specify the exact return type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method where I would like to return an object instance of
I have a script with a factory method that I would like to return
I would like to return an array of strings from C to Java using
Here is my problem. I would like to have a class with a private
Ideally I would like to send an object of type ArrayList<ArrayList<ASimpleClass>> from a remote
Comming from Java I would like to implement the following test multiple implementations idiom
I would like to return a string with all of the contents of a
I would like to return the id of a newly created object for my
I would like to return an array of string in my web services I've
Given the following XML, I would like to return all eventtitles where the eventtype

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.