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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:12:22+00:00 2026-06-03T09:12:22+00:00

I am creating a database of articles written by more than one authors, so

  • 0

I am creating a database of articles written by more than one authors, so I have two classes: Author and Article.
The constructor for Article is

        Article(String title, String venue, Author[] authors, long year).

An Author object contains a String with the name of the author and an ArrayList of articles he has written.

So, I have an array of Articles, and I’m trying to create an ArrayList of Authors and add all the articles they have written.

This is my code:

for(int i=0; i<allarticles.length; i++) {
            Author[] tempauthors = allarticles[i].getAuthors();
            for (int j=0; j<tempauthors.length; j++) {
                Author tempauthor = tempauthors[j];
                if (authors.contains(tempauthor)) {
                    Author oldAuthor = authors.get(authors.indexOf(tempauthor));
                    if (!oldAuthor.hasArticle(allarticles[i]))
                        oldAuthor.addArticle(allarticles[i]);
                } else {
                    if (!tempauthor.hasArticle(allarticles[i]))
                        tempauthor.addArticle(allarticles[i]);
                    authors.add(tempauthor);
                }
            }
        }

and here’s the hasArticle method:
public boolean hasArticle(Article a) {
return articles.contains(a);
}

I modified the equals method as suggested but now the problem is that I get authors with the right amount of articles but the first one is duplicated. What I am doing wrong? Should I ovverride the Article.equals() method as well?

  • 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-03T09:12:24+00:00Added an answer on June 3, 2026 at 9:12 am

    Unless the Author class has its own mechanism for returning the same Author object for each instance of an Author object where the name is the same, the code is working correctly.

    The contains() method doesn’t “know” that you’re referring to an author by name, it only knows that you’ve asked if the specific Author object is in the ArrayList.

    The contains() method compares the entire object using its equals(Object a) method. Two different objects are equal, if and only if, the equals() methods says they are. For a class without an explicit equals() method (other than the one inherited from Object or another class your class extends), the address (pointer …) is used.

    So, you have to define what “equals()” means. In your case, two objects are equal if the name of the authors are equal. In your case, that means equals() is this method —

    public boolean equals(Object a) {
      if (! (a instanceof Author))
        return false;
    
      return this.getname().equals(((Author) a).getname());
    }
    

    You also seem to have some funky logic in your code. You want to add the new article to the existing articles the author has. So you need to find the existing entry for that author (indexOf() method in ArrayList), get that entry (get() method …) then add the article to the Author object already in the ArrayList.

    To do this, you’ll need to do

    Author oldAuthor = authors.get(authors.indexOf(tempauthor));
    oldAuthor.addArticle(allarticles[i]);
    

    instead of the remove / addArticle / remove code you have.

    You will also need to make sure that you aren’t adding an article to the author’s list of articles that you’ve already added.

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

Sidebar

Related Questions

I'm creating a site that is going to have a wiki article database. Right
I'm creating a database table and I don't have a logical primary key assigned
I am creating a database that will help keep track of which employees have
I'm doing this tutorial: http://www.phpeveryday.com/articles/Zend-Framework-Database-Creating-Input-Form-P494.html We are building a simple input form using POST
I was wondering if you where to have an article or articles with huge
Basically I have articles in my database and I want to alter the way
I have a web application where users can create and edit articles. Each article
When creating database tables I'm often stumped when trying to name my time/date/timestamp fields.
I am creating database at runtime and I want to create the tables in
I'm creating a database application using ASP.NET and I want to make a Windows

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.