1

Wherever I try to define an array list in Processing (java) I get this syntax error:

Unexpected token: <

As of code:

ArrayList<byte> graph;


void setup() {
   // List serial ports and chose the first one
   println(Serial.list());
   myPort = new Serial(this, Serial.list()[0], 9600);
   //Initialise the byte array
   graph = new ArrayList<byte>();
}

I'm new to java, byt this is the syntax I have found in official docs, so there must be some issue with Processing implementation.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • You shouldn't really declare your graph as the implementation class unless you specifically need a method on that class. I'm guessing List will be sufficient. Otherwise, you're trying your entire class to an implementation and could be painful if you want to support something like LinkedList later. – Kurtymckurt Jun 17 '13 at 18:36

2 Answers2

3

When using primitives (e.g. int, short, byte) with generics, the "boxed type" (e.g. Integer, Short, Byte) must be declared. A "boxed type" or "wrapper type" is just the object-oriented representation of a primitive type.

In your case:

ArrayList<Byte> graph;

Adding and removing from the list may still be done with primitives. This is because Java will use auto-boxing to assist with transforming primitives to and from their object form.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
2

You cannot define List of byte primitive type , you need to use Byte wrapper class. Anything that is used as generics has to be convertible to Object.

ArrayList<Byte> graph;

See this answer as to why you cannot have primitive types as generics.

That is one of the compilation error you should get , but it won;t account for the error Unexpected token: < I guess . Something is wrong with your code which you have not posted.

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164