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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:33:08+00:00 2026-05-23T08:33:08+00:00

I want to navigate into a list by identifier . 1- I manage/create a

  • 0

I want to navigate into a list by identifier.

1- I manage/create a list.

2- I create function to get next item of a identifier element from my list

Can you help me to fix this code?

Prepare the list

List<String> myList = new ArrayList<String>();
myList.add("1");
myList.add("2");
myList.add("3");
myList.add("4");
myList.add("5");


public String function getNext(String uid) {

    if (myList.indexOf(uid).hasNext()) {
        return myList.indexOf(uid).nextElement();
    }
    return "";
}

public String function getPrevious(String uid) {
    return myList.indexOf(uid).hasPrevious() ? myList.indexOf(uid).previousElement() : "";
}
  • 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-23T08:33:09+00:00Added an answer on May 23, 2026 at 8:33 am

    You could use an index to lookup your String which is faster and simpler however to implement the functions as you have them.

    public String getNext(String uid) {
        int idx = myList.indexOf(uid);
        if (idx < 0 || idx+1 == myList.size()) return "";
        return myList.get(idx + 1);
    }
    
    public String getPrevious(String uid) {
        int idx = myList.indexOf(uid);
        if (idx <= 0) return "";
        return myList.get(idx - 1);
    }
    

    Using a List.get(i) is O(1) which makes keeping the index the fastest option. List.indexOf(String) is O(n). Using a NavigatbleSet might appear attractive as it is O(log n), however the cost of creating an object is so high that the collection has to be fairly large before you would see a benefit. (In which case you would use the first option)

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

Sidebar

Related Questions

I want to make a site where there user can basically navigate the web
I want to navigate to the N-th level of an object, and serialize it's
If I want to navigate directly to the second state of some flex app,
I want to conditionally navigate to some page. If some condition is true i
When I use Emacs I want to be able to easily display and navigate
In WPF, I want to create a hyperlink that navigates to the details of
After complete of asynchronous call to WCF service I want set success message into
Hope you all will be fine. Actually i want to upload image files from
I want to a tool which can convert c/c++ source code files to HTML
I want to use some kind of webbrowser object in java to navigate to

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.