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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:33:45+00:00 2026-06-18T06:33:45+00:00

this is the code I have written in Java, itr is the iterator for

  • 0

this is the code I have written in Java, itr is the iterator for the key, I am facing a problem in iterating the key, the question for the code is :We are given an array of 2n integers wherein each pair in this array of integers represents the year of birth and the year of death of a dinosaurs respectively. The range of valid years we want to consider is [-100000 to 2005]. For example, if the input was:

-80000 -79950 20 70 22 60 58 65 1950 2004

it would mean that the first dinosaur had a birth year of –80000 and an year of death of –79950 respectively. Similarly the second dinosaur lived from 20 to 70 and so on and so forth.

We would like to know the largest number of dinosaurs that were ever alive at one time. Write a method to compute this, given the above array of 2n integers.

Write one of the following methods:

C/C++:
int find_max_dinosaur (int[] years);

Java:
public int findMaxdinosaur (int[] years);

package usingarray;


int dinoStrength;
int deathOfDino;
int dinoBirthAge;
int currentDinoBirthAge;
int currentDinoDeathAge;
int count;

TreeMap dino=new TreeMap();
List<Integer> dinos = new ArrayList<Integer>();

Scanner sc=new Scanner(System.in);


public void getDionoAges()
{
    System.out.println("How many Dinosaur");
    dinoStrength=sc.nextInt()+1;
    for(int i=1;i<dinoStrength;i++)
    {
        System.out.println("Please enter "+i+" dino birth age and death age:");
        dino.put(sc.nextInt(),sc.nextInt());
    }
}

public void logic()
{
    Collection value = dino.values();
    Collection key = dino.values();
    Iterator it = value.iterator();
    Iterator itr = key.iterator();

    System.out.println("logic");

    for(int x=0;x<dinoStrength;x++)
    {
        System.out.println("in for");
    while(it.hasNext() && itr.hasNext())
        {
            System.out.println("in while");
            deathOfDino=(int) it.next();
            //currentDinoDeathAge=(int) it.next();
            dinoBirthAge=(int) itr.next();

            while(itr.hasNext())
            {
                System.out.println("In itr while");
                currentDinoBirthAge=(int) itr.next();
                if(dinoBirthAge<=currentDinoBirthAge && currentDinoBirthAge<=deathOfDino)
                {
                    count++;
                    System.out.println(count);
                }
            }
            dinos.add(count);
        }
    }
}

public void display()
{
    Iterator<Integer> it = dinos.iterator();
    while(it.hasNext())
    {
        System.out.println(it.next());
    }
}
public static void main(String[] args) {
    VickyDino vd=new VickyDino();
    vd.getDionoAges();
    vd.logic();
    vd.display();
}

}

  • 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-18T06:33:46+00:00Added an answer on June 18, 2026 at 6:33 am

    I would assume your problem is this:

    Collection value = dino.values();
    Collection key = dino.values();
    

    You’re using dino.values() twice; the second should probably be dino.keys(). However, since you’re putting the birth year into the keys and the year of death into the values, you probably may wanna switch the two. Note: I didn’t check whether the logic in your loops is correct.

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

Sidebar

Related Questions

In our application, I have seen code written like this: User.java (User entity) public
I have written this code in Java on my 64 bit Linux system: class
i have written a code in java as given under public class sstring {
Possible Duplicate: How do I compare strings in Java? I have written this code:
We have written a bunch of Java code and need to package this code
I have written this code for servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public
I have written this code inside a servlet to delete certain records from three
I have written this code in JavaScript and works perfectly fine when I include
I have written this code what it does is if user types postcode or
I have written this code outside all functions: int l, k; for (l =

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.