We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › ArrayList frustration "cannot cast from Object..."
Page Index Toggle Pages: 1
ArrayList frustration "cannot cast from Object..." (Read 2762 times)
ArrayList frustration "cannot cast from Object..."
Mar 30th, 2009, 2:26pm
 
In my sketch, I'm constructing an ArrayList containing ArrayLists that contain ints.  I can construct it just fine, but Processing won't let me get the data back out later.

Simple reproduction of the problem:


ArrayList outerList;

void setup()
{
 outerList = new ArrayList();
 
 for(int i = 0; i < 5; i++)
 {
   ArrayList innerList = new ArrayList();
   for(int j = 0; j < 5; j++)
     innerList.add((int)random(0, 100));
   outerList.add(innerList);
 }
 
 noLoop();
}

void draw()
{
 for(int i = 0; i < outerList.size(); i++)
 {
   print("list #" + nf(i,0) + ": ");
   ArrayList innerList = (ArrayList)(outerList.get(i));
   for(int j = 0; j < innerList.size(); j++)
   {
     int val = (int)innerList.get(j);
     print(nf(val,0) + " ");
   }
   println("");
 }
}

All this code does is construct one of these list-of-lists-of-ints, then attempt to print out the contents.  When I click the run button, I get "Cannot cast from Object to int" on this line:

int val = (int)innerList.get(j);

This has got to be something simple, right?
Re: ArrayList frustration "cannot cast from Object..."
Reply #1 - Mar 30th, 2009, 4:22pm
 
Try casting to 'Integer'. Ran into this a few days ago. Smiley
Re: ArrayList frustration "cannot cast from Object..."
Reply #2 - Mar 31st, 2009, 2:57am
 
Smart, NoahBuddy!
Explanation: Processing uses Java 1.5/1.6, so when we put int into an array list, auto-boxing kicks in and puts the int into an Integer. That's one feature of 1.5 we can use without changing syntax! I guess it won't work with 0135 for example.
Alas, since we can't type the ArrayList, it looks like the unboxing doesn't work, so we have to force the cast.
Re: ArrayList frustration "cannot cast from Object..."
Reply #3 - Mar 31st, 2009, 10:43am
 
Oh, for the love of pete!

Thanks, guys.  That worked.

But man, if I may rant for a second, that's astonishingly lame.  Having two different kinds of integer, depending on whether they're boxed or not?  Gimme a break.

Any idea if/when we're ever going to get real generic collection classes in Processing?

Re: ArrayList frustration "cannot cast from Object..."
Reply #4 - Mar 31st, 2009, 11:12am
 
cloister wrote on Mar 31st, 2009, 10:43am:
Oh, for the love of pete!

Thanks, guys.  That worked.

But man, if I may rant for a second, that's astonishingly lame.  Having two different kinds of integer, depending on whether they're boxed or not  Gimme a break.

Any idea if/when we're ever going to get real generic collection classes in Processing


It really isn't "lame" there's very good reasons for there being 2 versions. The main one is: speed. The primitive "int" is the thing a processor understands.. it's a set of bits which it can directly address, do maths on and ship around, and so is what you'll use normally. It's not "pure" where everything is an Object, but it is necessary to make things run fast.

But it's not an Object, it has no methods, can't be pointed at by two variables etc, so there's the Object version. And to save every single class ever written to handle generic things have to have 8 extra versions of each method for the 8 primitive types, there are Object types of the primitives, so you only ever need to know how to handle an Object if you don't care what exactly is in it.

So you have the ease of use of an ArrayList, you just have to wrap the int into an Integer
Re: ArrayList frustration "cannot cast from Object..."
Reply #5 - Apr 1st, 2009, 10:34am
 
Well--and at the risk of invoking the "M-word" and starting some sort of holy war--the .NET framework originally came without true generic collection classes either.  It had an ArrayList class, too, that yielded Object as the return type for element accesses.  But it allowed me to cast back to regular old 'int' just fine.

Given that both .NET and Java are JIT'ed VM systems, I don't really see why Java has to make this so hard.  Surely Sun could have, in the years since .NET was released, figured out how to do this trick themselves...
Re: ArrayList frustration "cannot cast from Object..."
Reply #6 - Apr 1st, 2009, 3:18pm
 
Excuse me, cloister, but I think you are barking at the wrong tree (I find the expression amusing! Smiley)
Java is doing unboxing correctly if the array list is typed. As I wrote, the limitation is in Processing, which cannot use 1.5 syntax yet (waiting for somebody to hack the Antlr syntax file to fit P5 needs...).
I wrote a simple class put in a .java file in a Processing sketch:
Code:
import java.util.ArrayList;

class SomeTestClass
{
ArrayList<Integer> al = new ArrayList<Integer>(10);

SomeTestClass()
{
for (int i = 0; i < 10; i++)
{
al.add(i * 10);
}
}
void Print()
{
for (int i = 0; i < 10; i++)
{
System.out.println(al.get(i) / 5);
}
}
}

In my sketch, I just do:

SomeTestClass ta = new SomeTestClass();
ta.Print();

And it works fine...
Re: ArrayList frustration "cannot cast from Object..."
Reply #7 - Apr 2nd, 2009, 10:42am
 
Yes, that would work fine.  But as you say, Processing doesn't yet support the true generic ArrayList collection yet.  In either (modern) Java or .NET 2.0 or later, ArrayList<int> does what it ought to do, and unboxes the result for you when you query.  My gripe is that in .NET 1.1, which didn't support generics, this worked:

ArrayList A = new ArrayList();
A.add(7);
int x = (int)(A[0]); // C# syntax uses [] instead of .get()

And in the current Processing, which also doesn't support generics, this doesn't work:

ArrayList A = new ArrayList();
A.add(7);
int x = (int)(A.get(0));

When you would really think that it ought to.  That's all.  I'm glad there's an easy work-around.  Like I said, it was just a rant.
Page Index Toggle Pages: 1