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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:30:08+00:00 2026-05-15T19:30:08+00:00

List<T> list = new ArrayList<T>(); 1 method: for(int i = list.length – 1; i

  • 0
List<T> list = new ArrayList<T>();

1 method:

for(int i = list.length - 1; i >= 0; i--) {
  System.out.println(list.get(i));
}

2 method:

for(T t : list) {
  System.out.println(t);
}

3 method:

Iterator<T> it = list.iterator();     
while(it.hasNext()) {
  System.out.println(it.next());
}
  • 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-15T19:30:09+00:00Added an answer on May 15, 2026 at 7:30 pm

    The efficiency is unlikely to be significant – certainly System.out.println is much more likely to be the bottleneck in your particular example.

    The second approach (enhanced for loop) is the most readable however. Note that the three approaches don’t do the same thing – the first one will iterate from the end rather than the start. Getting the right behaviour almost always trumps running a teeny, tiny bit faster. The more readable your code is, the more likely you are to get it right.

    Go for readability, measure the performance of your app, and if it becomes a problem, micro-optimise the bottlenecks (continuing to measure at every step).

    EDIT: I was basing this answer on the first line of the question, showing an ArrayList<T> being used.

    If you want an answer for any List<T> then there simply isn’t an accurate answer. List<T> doesn’t offer any guarantees about complexity. It doesn’t say how fast you should expect get to be, nor how fast you should expect an iterator to be. You could be using an implementation which has good random access, but a painfully slow iterator. It’s unlikely, but it would still be a valid List<T>.

    In short: if you’re concerned about the efficiency, you need to know what kind of list you’re using. In general iterating it likely to be reasonably efficient, and random access may or may not be efficient. (It may be more efficient than iterating, but it’s unlikely to be significantly more efficient.) In practice, my experience is that in most applications you actually have enough knowledge to make a reasonable judgement… but I’d still usually code for readability first.

    As others have pointed out, if you do want to use random access to get at the elements, then it would be worth ensuring that your list implements the RandomAccess interface too.

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

Sidebar

Related Questions

public static <T> List<T> repeat(T contents, int length) { List<T> list = new ArrayList<T>();
Possible Duplicate: list.clear() vs list = new ArrayList<int>(); I have a list: List<Integer> l1
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i <
ArrayList<decimal> FindSumSubset(decimal sum, ArrayList<decimal> list) { for (int i = 0; i < list.Length;
This is the code ArrayList<MyObject> list = new ArrayList<MyObject>(); list.add(new MyObject()); Intent intent =
Say I have a List like: List<String> list = new ArrayList<>(); list.add(a); list.add(h); list.add(f);
I have the below java code: List<SomePojo> list = new ArrayList<SomePojo>(); //add 100 SomePojo
Here is a simple sorting program of an ArrayList: ArrayList<String> list = new ArrayList<String>();
I'm trying to make a list containing different objects. List<Object> list = new ArrayList<Object>();
I have a List of HashMap such as below ArrayList l = new ArrayList

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.