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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:53:41+00:00 2026-05-27T09:53:41+00:00

Given the following code : public void insertIntoQueue(float length,int xElement,int yElement,int whichElement) { Dot

  • 0

Given the following code :

public void insertIntoQueue(float length,int xElement,int yElement,int whichElement)
    {
        Dot dot = new Dot(xElement,yElement);
        GeometricElement element = null;

        // some code 

        int robotX,robotY;
        boolean flag = false;
        for (Iterator<Robot> i = robotList.iterator(); i.hasNext();)
        {

            // Robot currentRobot = (Robot) i.next();           

             robotX = ((Robot)(i)).getXlocation();
             robotY = ((Robot)(i)).getYlocation();

        // more code , irrelevant 
    }

I have the following objects : Robot,GeometricElement and Dot .

I want to iterate on a Robot linked list which defined as:

public class Ground {

    // more fields 

    private LinkedList <Robot> robotList;  // used for storing the robots 
    public Ground(int row,int col)   // ctor 
{
            // some code 

    this.robotList = new LinkedList<Robot>();
}
}

but the line : robotX = ((Robot)(i)).getXlocation();
and robotY = ((Robot)(i)).getYlocation();
throws an exception of dispatchUncaughtException .

Please pay attention that I don’t want to remove elements from the linked list,
what I need is to get fields from a current element with the iterator.

So what’s wrong ?

Regards
Ron

  • 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-27T09:53:41+00:00Added an answer on May 27, 2026 at 9:53 am

    Your commented out line is actually the correct line, except remove the cast:

    Robot currentRobot = i.next();           
    

    Because your iterator is typed, you dont need the cast and the compiler ensures you’re working with the right kind of object.

    After that, you can simply:

    robotX = currentRobot.getXlocation();
    robotY = currentRobot.getYlocation();
    

    No ugly casts!

    BTW, if you don’t need to modify the collection via the iterator, you can improve the code style considerably, buy using a “foreach”:

    for (Robot currentRobot : robotList) {
        robotX = currentRobot.getXlocation();
        robotY = currentRobot.getYlocation();
        // .. more code
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following Code. [TestMethod] public void CanResolveILoggerTest() { var Container = new Castle.Windsor.WindsorContainer();
Given the following code : package core; public abstract class GeometricElement { private float
In c#, Given the following code: public class Person { public int PersonID {
Given the following code: public interface Selectable { public void select(); } public class
Given the following code sample: public class WeirdStuff { public static int doSomething() {
Given the following code, I try to implement the public static Point operator +(int
Given the following code: class monomial { public: mp_real coe; int exp; monomial *next;
Given the following code class T { public: virtual ~T () {} virtual void
Given the following code : import java.io.*; public class Main { public static void
Given the following code: #pragma once class B { public: B(void) { } ~B(void)

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.