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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:23:00+00:00 2026-06-08T20:23:00+00:00

Based on a question , the user wanted to access 99999th line of a

  • 0

Based on a question, the user wanted to access 99999th line of a 100000 lines file without having to iterate using an eachLineclosure on the first 99998 lines. So, I had suggested that he use

file.readLines().reverse()[1] to access the 99999th line of the file.

This is logically appealing to a programmer. However, I was quite doubtful about the intricacy regarding the implementation of this method.

Is the reverse() method a mere abstraction of the complete iteration on lines that is hidden from the programmer or is it really as intelligent as to be able to iterate over as less number of lines as possible to reach the required line?

  • 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-06-08T20:23:01+00:00Added an answer on June 8, 2026 at 8:23 pm

    As you can see from the code, reverse() calls Collections.reverse in Java to reverse the list.

    However the non-mutating code gives you another option. Using listIterator() you can get an iterator with hasPrevious and previous to walk back through the list, so if you do:

    // Our list
    def a = [ 1, 2, 3, 4 ]
    // Get a list iterator pointing at the end
    def listIterator = a.listIterator( a.size() )
    // Wrap the previous calls in another iterator
    def iter = [ hasNext:{ listIterator.hasPrevious() },
                 next:{ listIterator.previous() } ] as Iterator
    

    We can then do:

    // Check the value of 1 element from the end of the list
    assert iter[ 1 ] == 3
    

    However, all of this is an ArrayList under the covers, so it’s almost certainly quicker (and easier for the code to be read) if you just do:

    assert a[ 2 ] == 3
    

    Rather than all the reversing. Though obviously, this would need profiling to make sure I’m right…

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

Sidebar

Related Questions

Quick question. If I wanted to say, have a login button if user isn't
Quick question. If I wanted to say, have a login button if user isn't
Hi I found this questions How to manipulate WPF GUI based on user roles
Sorry for this based question. I am fresh to jqplot and would like to
I am working on a project which is basically a knowledge based question answering
This is a question based on: Trying to get tag-it to work with an
This is a further question based on this answer: How can I implement a
I have a three part question based on a dataframe (df is example rows)
So, my second question based off of this application I'm teaching myself Objective C
Based on this question I've created a small application which is catching all debug

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.