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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:35:51+00:00 2026-05-17T14:35:51+00:00

Here is my object constructor static class Edge { int source; // source node

  • 0

Here is my object constructor

static class Edge {
    int source; // source node
    int destination; // destination node
    int weight; // weight of the edge
    int predecessor; // previous node
    public Edge() {};
    public Edge(int s, int d, int w) { source = s; destination = d; weight = w; }
}

Now, here is the statement where I create a new Edge object

edges.add(new Edge(nodeStart, tempDest, tempWeight));

I need to skip over that statement if there has already been an object created with the same parameters (nodeStart, tempDest)

Basically, I don’t want to add the same edge twice.

edges.add(new Edge(0, 1, tempWeight));
edges.add(new Edge(0, 1, tempWeight));

If that happens, I want to make sure it only adds it once, and not new identical objects.

  • 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-17T14:35:51+00:00Added an answer on May 17, 2026 at 2:35 pm

    The proper way would be to make edges a Set<Edge>. And then properly override hashcode and equals.

    So, you should declare edges like so:

    Set<Edge> egdes = new HashSet<Edge>();
    

    And you should implement hashCode and equals like so:

    public boolean equals(Object o) {
        if (o == null) return false;
        if (o == this) return true;
        if (!(o instanceof Edge)) return false;
        Edge oEdge = (Edge) o;
        return this.source == oEdge.source && 
               this.destination == oEdge.destination && 
               this.weight == oEdge.weight;
    }
    
    public int hashCode(){
        return source * 3 + dest * 11 + weight * 13;
    }
    

    You should read up on properly implementing equals and hashCode. My examples are not great. But the general idea is conveyed.

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

Sidebar

Related Questions

Here's my binding source object: Public Class MyListObject Private _mylist As New ObservableCollection(Of String)
I have a static class that creates several worker threads in its constructor. If
Here is my SQLCommand object: oCommand.CommandText = INSERT INTO hits (id,client_id,client_ip,page,vars) VALUES _ (@@IDENTITY,@client_id,@ip,@page,@vars)
here is what a I'm doing: object ReturnMatch(System.Type type) { foreach(object obj in myObjects)
Here's the scenario-- you've got a JSON object on the browser, and let's say
I'm missing something here: $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry $objSearcher.Filter =
Here is a problem I've struggled with ever since I first started learning object-oriented
Object-relational mapping has been well discussed, including on here. I have experience with a
Here's a simple method with a foreach loop: IEnumerable<XElement> FieldsToXElements(object instance) { var fieldElements
Not sure what's going on here. I have a DateTime object, and when I

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.