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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:25:25+00:00 2026-06-06T14:25:25+00:00

I need your help with some sample code for a situation I could not

  • 0

I need your help with some sample code for a situation I could not get free from.
I have a simple list of objects. My class is like this:

class MyClass {
    String str;
    Integer intgr;
}

And the list contains elements like:

[{a1  5}, {b2  3}, {g1  1}, {b5  1}, {c9  11}, {g2  3}, {d1  4}, {b3  19}... ... ...]

I need to check if any element contain the same prefix in string (here suffix is the last single character) then keep that element which have greater value in integer. The expected output from the above example list will be:

[{a1  5}, {c9  11}, {g2  3}, {d1  4}, {b3  19}... ... ...]

Strings will have unique values but could have matches in prefix. I’m not that good in java. So can anybody help me out from this? Here is the code I’m trying but getting IndexOutOfBoundsException. This code has faults, so need some help from you.

Thanks!

        int size = list.size();
        for (int j = 0; j < size; j++) {
        if (list.get(j).str.substring(0, list.get(j).str.length()-1).compareTo(list.get(j+1).str.substring(0, list.get(j+1).str.length()-1)) == 0) {
            if (list.get(j).intgr > list.get(j+1).intgr)
                list.remove(list.get(j+1));
                size--;
            else {
                list.remove(list.get(j));
                j--;
                size--;
            }
        }
    }
  • 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-06T14:25:27+00:00Added an answer on June 6, 2026 at 2:25 pm

    There are two problems with your code. First, when j == size - 1 (the last iteration), you are calling list.get(j+1), which is what is causing the exception. Just change your loop condition to j < size - 1 and the exception should go away. (Alternatively, start at j = 1 and compare to the previous element.)

    Second, you are only comparing each element with its immediate successor element. From your description, it doesn’t sound like that’s what you want to do.

    I’d suggest capturing the logic of comparison in a separate method. It might be part of MyClass:

    class MyClass {
        String str;
        Integer intgr;
        /**
         * Returns the relationship between this and another MyClass instance.
         * The relationship can be one of three values:
         * <pre>
         *   -1 - This object should be discarded and other kept
         *    0 - There is no relationship between this and other
         *    1 - This object should be kept and other discarded
         * </pre>
         *
         * @param other The other instance to evaluate
         *
         * @return 0 if there is no relationship.
         */
        public int relationTo(MyClass other) {
            final String myPrefix = str.substring(0, str.length() - 1);
            final String otherPrefix = other.str.substring(0, other.str.length() - 1);
            if (myPrefix.equals(otherPrefix)) {
                return intgr < other.intgr ? -1 : 1;
            } else {
                return 0;
            }
        }
    }
    

    (This can easily be transformed into a method with two arguments that would be outside MyClass.) Then you can use the method to decide what to keep. You need to do a double iteration to find non-adjacent objects:

    int size = list.size();
    for (int i = 0; i < size; ++i) {
        final MyClass current = list.get(i);
        for (int j = 0; j < i; ++j) {
            final MyClass previous = list.get(j);
            final int relation = previous.relationTo(current);
            if (relation < 0) {
                // remove previous (at index j)
                list.remove(j);
                --i;
                --j;
                --size;
            } else if (relation > 0) {
                // remove current (at index i)
                list.remove(i);
                --i;
                --size;
                break; // exit inner loop
            }
            // else current and previous don't share a prefix
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need your help in JAVA (with some sample code if possible) regarding to
I need your help with display of some comma separated enteries from my database.
I need your help. Say I have a code inside a for statement similar
I have some very simple sample code like this: $.ajax({ url: 'demo2.htm', success: function(loadeddata){
Need your help to get the max of CAP_PRICE based on certain criteria in
need your help with PHP templating. I'm new to PHP (I'm coming from Perl+Embperl).
I have a situation where i need to prevent users from explicitly calling say
I'm working on a simple game and I need some help to improve my
Please i need some help in adding items to a JComboBox in Java from
I need your help. I tried to play an audio file stored in Assets

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.