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

  • Home
  • SEARCH
  • 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 582997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:44:46+00:00 2026-05-13T14:44:46+00:00

I am currently rediscovering Java (working with Ruby a lot recently), and I love

  • 0

I am currently rediscovering Java (working with Ruby a lot recently), and I love the compilation-time checking of everything. It makes refactoring so easy. However, I miss playing fast-and-loose with types to do an each loop. This is my worst code.

Is this as short as it can be? I have a collection called looperTracks, which has instances that implement Looper. I don’t want to modify that collection, but I want to iterate through its members PLUS the this (which also implements Looper).

List<Looper> allLoopers = new ArrayList<Looper>(looperTracks.length + 1);
for (LooperTrack track : looperTracks) {
    allLoopers.add(track);
}
allLoopers.add(this);

for (Looper looper : allLoopers) {
    // Finally! I have a looper

I’m particularly concerned about any features that are new to Java from 1.5 on that I may have missed. For this question I am not asking about JRuby nor Groovy, though I know that they would work for this.

Edit: Sorry (too much Ruby!)… looperTracks is of type LooperTrack[] and LooperTrack implements Looper.

  • 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-13T14:44:47+00:00Added an answer on May 13, 2026 at 2:44 pm

    There are at least two possible built-in ways to shorten your code:

    You could use Collection.addAll(Collection) that appends each element in the collection passed as parameter to the end of the collection.:

    List<Looper> allLoopers = new ArrayList<Looper>();
    
    ...
    
    allLoopers.addAll(looperTracks);
    allLoopers.add(this);
    
    for(Looper looper : allLoopers) {
      ...
    }
    

    or you can use a constructor that takes a collection as a parameter:

    List<Looper> allLoopers = new ArrayList<Looper>(looperTracks);
    

    Due to the change of question: All arrays can easily be converted to collections using java.util.Arrays e.g.

    List<Looper> someLooperTracks = Arrays.asList(looperTracks). 
    

    This will wrap the array in a fixed-size list.

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

Sidebar

Ask A Question

Stats

  • Questions 295k
  • Answers 295k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The size of the signature is the size of the… May 13, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer Since you are already using the Form tag, having the… May 13, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer I finally found the interface that seems to allow this:… May 13, 2026 at 6:50 pm

Related Questions

I have two vectors of floats and i want them to become one vector
I am currently aware that ASP.NET 2.0 is out and about and that there
I am currently working on a project and my goal is to locate text
I am currently using Windows Server 2008 Standard and have several Hyper V machines.
I am currently using the following command to upload my site content: scp -r

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.