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 6611735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:00:32+00:00 2026-05-25T20:00:32+00:00

Is there some interface that will allow for iteraction of arrays and java.util.Lists? Seeing

  • 0

Is there some interface that will allow for iteraction of arrays and java.util.Lists?

Seeing as how the for-each loop can iterate both it seems to me there must be some sort of base ‘iterable’ that is being traversed in both cases.

So, can i setup a method parameter that accepts either Foo[], or List?

We have lots of old code (which we won’t be updating), that calls a few low level methods (which i’d like to update) that are expecting arrays.

Our new code is converting from Lists (specifically ArrayLists most of the time) to arrays using List’s toArray method, and i’d like to be able to skip this step.

I’ve seen this post which shows the implementation, but I’m not sure how it might help.

Can this be done?

  • 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-25T20:00:32+00:00Added an answer on May 25, 2026 at 8:00 pm

    No, the language specification explicitly calls out arrays separately. Arrays don’t implement Iterable<T>, which is the "normal" interface used by the enhanced for loop other than for arrays. From section 14.14.2 of the JLS:

    The enhanced for statement has the form:

     EnhancedForStatement:
           for ( VariableModifiers Type Identifier: Expression) Statement
    

    The Expression must either have type Iterable or else it must be of an array type (§10.1), or a compile-time error occurs.

    The two cases are then handled separately.

    Converting from arrays to lists is easy of course – but it sounds like you need to do this the other way round. There’s really no "pleasant" way of doing this – and creating the array requires copying everything, of course.

    How much work would it be to convert at least the most heavily used bits of the API to use lists?

    You can write method overloads of course, e.g.

    public void foo(String[] array)
    {
        foo(Arrays.asList(array));
    }
    
    public void foo(List<String> list)
    {
        // Use the list
    }
    

    … but doing that for your old code will still involve work, as the real implementation is in terms of the list, not the array.

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

Sidebar

Related Questions

I'm making a simple interface that will allow a user to modify pieces of
I want to make an attribute that will allow me to specify some method
I have a interface that defines some methods with attributes. These attributes need to
Is there some kind of a shorthand fluent interface for creating a parameters dictionary
Is there some way I can use URLs like: http://www.blog.com/team-spirit/ instead of http://www.blog.com/?p=122 in
I'm writing a Java application that will run for a long time (essentially, it
I want to use R inside my Java application. I now there is some
I have some code that listens for announcements via UDP multicast. I can get
Our company have a need to set up some solution, that would allow us
I am currently working on some older java code that was developed without App

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.