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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:31:10+00:00 2026-06-16T07:31:10+00:00

I’ve got objects ( Rank s) that are being added to a list as

  • 0

I’ve got objects (Ranks) that are being added to a list as another list is looped through.

Each of these objects has an “up” and a “down” property, which need to be set to the following and previous element, respectively. If the element is at the top of the list, the “up” property should point to itself, likewise for the bottom element.

I’m using a Vector (java.util.Vector) so I can use indices to find the following and previous elements.
Here is the adding process, with manipulating up/down:

public void addToRanksInOrderWithUpDown(Rank r) {
    ranksInOrder.addElement(r);
    if (ranksInOrder.size() != 1) {
        Rank ru, rd;
        try {
            ru = ranksInOrder.elementAt(ranksInOrder.indexOf(r)+1);
        } catch (ArrayIndexOutOfBoundsException e) {
            ru = r;
        }
        try {
            rd = ranksInOrder.elementAt(ranksInOrder.indexOf(r)-1);
        } catch (ArrayIndexOutOfBoundsException e) {
            rd = r;
        }
        r.setUp(ru);
        r.setDown(rd);
        ru.setDown(r);
        rd.setUp(r);
    }
}

r being an instance of a passed Rank. Here is the loop:

if (g.getRanksInOrder().size() == 1) {
    g.addToRanksInOrder(currentRank);
} else {
    g.addToRanksInOrderWithUpDown(currentRank);
}

addToRanksInOrder simply does the first step of addToRanksInOrderWithUpDown, ranksInOrder.addElement(r);.

Unfortunately, “up” and “down” often end up being Null or the wrong thing. What is a better way to go about this? Can I fix my code, or does it need to be scrapped?

Edit: sorry, but none of the answers actually work. The problem remains as before, with various ups/downs remaining null or the wrong thing.


Here’s an SSCCE:

public class Rank {
    private Rank up;
    private Rank down;
    private String name;
    public Rank getUp() {
        return up;
    }
    public Rank getDown() {
        return down;
    }
    public Rank getName() {
        return name;
    }
    public void setUp(Rank r) {
        up = r;
    }
    public void setDown(Rank r) {
        down = r;
    }
    public Rank(String s) {
        name = s;
    }
}

public class Ranker {
    private Set<Rank> ranks = new HashSet<Rank>();
    private Vector<Rank> ranksInOrder = new Vector<Rank>(); 
    public Vector<Rank> sort() {
        for (Rank r : ranks) {
            if (ranksInOrder.size == 1) {
                addToRanksInOrder(r);
            } else {
                addToRanksInOrderWithUpDown(r);
            }
        }
        return ranksInOrder;
    }
    private void addToRanksInOrderWithUpDown(Rank r) {
        ranksInOrder.addElement(r);
        if (ranksInOrder.size() != 1) {
            Rank ru, rd;
            try {
                ru = ranksInOrder.elementAt(ranksInOrder.indexOf(r)+1);
            } catch (ArrayIndexOutOfBoundsException e) {
                ru = r;
            }
            try {
                rd = ranksInOrder.elementAt(ranksInOrder.indexOf(r)-1);
            } catch (ArrayIndexOutOfBoundsException e) {
                rd = r;
            }
            r.setUp(ru);
            r.setDown(rd);
            ru.setDown(r);
            rd.setUp(r);
        }
    }
    private void addToRanksInOrder(Rank r) {
        ranksInOrder.addElement(r);
    }
    private String displayAsList(Vector<Rank> vr) {
        String list = "";
        for (Rank r : vr) {
            list += "~" + r.getName() + "\n";
            if (r.getUp() == null) {
                list += " +NULL\n";
            } else {
                list += " +" + r.getUp().getName() + "\n";
            }
            if (r.getDown() == null) {
                list += " -NULL\n";
            } else {
                list += " -" + r.getDown().getName() + "\n";
            }
            list += "----\n";
        }
        return list;
    }
    public static void main(String[] args) {
        for (int i=0;i<11;i++) {
            ranks.add(new Rank("Rank " + i));
        }
        System.out.println(displayAsList(sort()));
    }
}
  • 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-16T07:31:12+00:00Added an answer on June 16, 2026 at 7:31 am

    Try this

    void addToRanksInOrderWithUpDown(Rank r) {
        r.setUp(r);
        if (ranksInOrder.isEmpty()) {
            r.setDown(r);
        } else {
            Rank last = ranksInOrder.get(ranksInOrder.size() - 1);
            r.setDown(last);
            last.setUp(r);
        }
        ranksInOrder.add(r);
    }
    

    Notes:

    Using Vector seems weird, it’s a legacy class, use ArrayList instead.

    You should not use exceptions for control flow, it is a known anti-pattern. See “Effective Java” by J.Bloch, Item 57: “Use exceptions only for exceptional conditions”.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
I am confused How to use looping for Json response Array in another Array.
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

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.