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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:41:04+00:00 2026-05-26T11:41:04+00:00

NOTE TO READER: Thanks to Tim’s help below, I was able to resolve the

  • 0

NOTE TO READER: Thanks to Tim’s help below, I was able to resolve the problem. The root of the problem wasn’t the ArrayList casting, but the fact that the runtime engine pointed the error to the wrong line, i.e. the one I show below where I access the list. The real error was 4 lines below that, where I made a mistake assigning an element into the element I just retrieved.

I’ve got a small Result class (at bottom of my grails controller class), and then in my code, I do

ArrayList<Result> bestResults = new ArrayList<Result>()

to help the compiler know what bestResults is (but tried other forms below). Then, I try to access this arraylist, via:

for (int r = 0; r < topSet; r++) {
   Result res = bestResults[r] as Result

and, I get an “Cannot cast object … to blahblah.Result” run-time error.

I’ve tried other variations of both, namely:

ArrayList bestResults = new ArrayList() 

with

Result res = (Result) bestResults[r]

or just

Result res = bestResults.get(r) as Result

and I always get the same casting error. I just can’t seem to please the Groovy “engine” / JVM! 🙂 🙁

Can you please assist?

———————– The more full code —————–

The Result class is at the bottom of my grails controller (main) class, namely:

class Result {
   float factor
   def results = null // NOTE: this will hold an array of float
}

Then, in the controller, I do the following, intializing bestResults to bogus values:

 def results = new float[2 * deltaRange + 1] as float[] // the results array gets assigned to bestResults AFTER the error I get, so it seems non-relevant

 ArrayList<Result> bestResults = new ArrayList<Result>()
 Result fakeResult = new Result(factor: 100.0f, results: null)
 for (int r = 0; r < 10; r++) {
        bestResults.add(r,fakeResult)
 }

Then, when I go to insert a real result, I run into the problem, doing:

for (int r = 0; r < 10; r++) {
  Result res = bestResults.get(r) as Result

or doing

  Result res = bestResults[r] as Result

or

   Result res = bestResults[r]

All give the error:

Message: Cannot cast object '[F@4f5b6d' with class '[F' to class 'alwaysbetter.Result'

* After this error, I have the code that does:

bestResults[r].results =  results

where results is defined as an array of floats at the top, namely:

def results = new float[2 * deltaRange + 1] as float[] 
  • 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-26T11:41:05+00:00Added an answer on May 26, 2026 at 11:41 am

    Right, the problem is the last bit of information you gave…

    def results = new float[2 * deltaRange + 1] as float[] 
    

    makes no sense in groovy, to get a float array of 2 * deltaRange + 1 elements, your best bet is probably:

    float[] results = [ 0 ] * ( 2 * deltaRange + 1 )
    

    Original answer

    Is this inside Java code?

    When you say “main class”, what do you mean?

    How does this relate to grails?

    This works in groovy:

    class Result {
      String name
    }
    
    ArrayList<Result> bestResults = new ArrayList<Result>()
    
    bestResults << new Result( name:'tim' )
    
    for (int r = 0; r < bestResults.size(); r++) {
       Result res = bestResults[r]
       println res
    }
    

    Or, more groovily:

    bestResults.each { Result r -> println r }
    

    Edit after question update

    That error message says you are trying to convert an array of float (ie: float[]) into a Result object

    There must be something missing from the code you have posted, as if I paste it into a groovy console, it works fine

    It’s a bit odd though…

    You do realise you’re adding the same Result instance to the List 10 times don’t you? Not 10 separate instances?

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

Sidebar

Related Questions

In Google Reader, you can use a bookmarklet to note a page you're visiting.
Note that I am not asking which to choose (MVC or MVP), but rather
Note The question below was asked in 2008 about some code from 2003. As
Edit: I solved my problem but if you have anything to add please do.
I posted before but now I seem to actually see the problem, just having
[Note: I prefer PyQt, but an answer for Qt is the same as far
I would like to render a list of HTML links in ASP.NET MVC. Note
NOTE: XMLIgnore is NOT the answer! OK, so following on from my question on
Note: This was posted when I was starting out C#. With 2014 knowledge, I
NOTE: I am not set on using VI, it is just the first thing

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.