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 › LinkedList<primitive>
Page Index Toggle Pages: 1
LinkedList<primitive> ? (Read 1690 times)
LinkedList<primitive> ?
May 18th, 2010, 5:16pm
 
Hello,

I'm trying to use a LinkedList to contain only integers.
In the interest of avoiding myIntger = (Integer)myList.get(i) every time I use .get, I'd like to declare the list as:
LinkedList<Integer> myList = new LinkedList();

Weird thing is, that line works just fine within setup(), but when I place it before setup() so that it can be globally accessed, it causes "unexpected token: void" when I try to run.

Heres code to test:
void setup(){
 LinkedList<Integer> myList = new LinkedList();
 for (int i = 0;i<10;i++){
   myList.add(i);
   println(myList.get(i));
 }
 int myInteger = myList.get(1)+myList.get(2);
 println("my integer is "+myInteger);
}
void draw(){
}
works fine but myList is local to setup() which is fairly useless.
Move the myList declaration to before setup() and it fails.  Ideas?

Thanks,
Mike
Re: LinkedList<primitive> ?
Reply #1 - May 18th, 2010, 11:00pm
 
Quote:
LinkedList myList = new LinkedList();

void setup(){
 for (int i = 0;i<10;i++){
   myList.add(i);
   println(myList.get(i));
 }
 int myInteger = (Integer)myList.get(1)+(Integer)myList.get(2);
 println("my integer is "+myInteger);
}
void draw(){
}



[Edit - whoops!  huzzah for reading comprehension]
Re: LinkedList<primitive> ?
Reply #2 - May 19th, 2010, 2:26am
 
BenHem, OP wrote "In the interest of avoiding myIntger = (Integer)myList.get(i) every time I use .get, " Wink

miked3, you must write:
LinkedList<Integer> myList = new LinkedList<Integer>();
ie. you must repeat the type in the instantiation. Yes, that's heavy...
Re: LinkedList<primitive> ?
Reply #3 - May 19th, 2010, 10:08am
 
LinkedList<Integer> myList = new LinkedList<Integer>();

void setup(){
}

gives "unexpected token: <"
Re: LinkedList<primitive> ?
Reply #4 - May 19th, 2010, 10:17am
 
Also, is using BenHem's suggested code any less efficient than declaring the whole list as <Integer>?

Thanks for the replies.
-mike
Re: LinkedList<primitive> ?
Reply #5 - May 19th, 2010, 11:54am
 
miked3 wrote on May 19th, 2010, 10:08am:
LinkedList<Integer> myList = new LinkedList<Integer>();

void setup(){
}

gives "unexpected token: <"

I think what's happening is that Processing tries to replace every occurence of LinkedList with LinkedList<Object> before compiling. So, when you type:
Code:

LinkedList<Integer>

the compiler sees:
Code:

LinkedList<Object><Integer>

which generates the error you're getting.

One way to make it a little more convenient would be to write a wrapper method for myList.get() that does the cast for you:
Code:
Integer listGet(int i) {
return (Integer) myList.get(i);
}


Other than that, the only way I can think of to get around this is to switch from the PDE to Eclipse.
Re: LinkedList<primitive> ?
Reply #6 - May 20th, 2010, 1:02am
 
miked3 wrote on May 19th, 2010, 10:08am:
LinkedList<Integer> myList = new LinkedList<Integer>();

void setup(){
}

gives "unexpected token: <"

It works for me...
Then again, I use Processing 0184, which I assumed you were using.
If you use Processing 1.1, you don't have access to this syntax.
Page Index Toggle Pages: 1