Java List generics syntax for primitive types?

Use the wrapper class Byte: List mylist = new ArrayList(); Then, because of autoboxing, you can still have: for (byte be : mylist) { }.

List is an interface, so you can't new one up. You'd need to use a concrete class instead, such as ArrayList or LinkedList – Michael Williamson Feb 20 '10 at 11:30 @Michael Williamson a typo, rather than me not knowing that ;) – Bozho Feb 20 '10 at 11:35 Thanks Bozho that worked for me. – Ciarán Feb 20 '10 at 11:43.

You have a Byte class provided by the JRE. This class is the corresponding class for the byte primitive type. See here for primitive types.

You can do this : List myList = new ArrayList(); byte be = 127; myList. Add(b); be = 0; // resetting be = myList. Get(0); // => be = 127 again As Michael pointed in the comments : List myList = new ArrayList(); Byte be = null; myList.

Add(b); byte primitiveByte = myList. Get(0); results in : Exception in thread "main" java.lang. NullPointerException at TestPrimitive.

Main(TestPrimitive. Java:12).

1 don't give links to 1.4.2 – Bozho Feb 20 '10 at 11:29 1 It's probably worth adding that generics and primitives don't play nicely together -- that is, you can't use a primitive as a type argument, hence the need to use Byte instead of byte. Autoboxing isn't perfect -- a Byte can be null, where as a byte can't, so trying to assign a Byte to a byte can result in a null pointer exception. – Michael Williamson Feb 20 '10 at 11:34 @Bozho: They still show up as the first results on Google or Bing.

One nice example where search engines suck ;-) – Joey Feb 20 '10 at 11:39 1 yes, they do. That's why I type "java.lang. Something 6" – Bozho Feb 20 '10 at 11:40 I always edit the URL (j2se/1.4.2 => javase/6) before posting links to the docs.

– finnw Feb 20 '10 at 16:44.

You could also use TByteArrayList from the GNU Trove library.

More upvotes guys, the only performant solution suggested, and you just... – Anton S. Kraievoy Mar 10 at 10:31.

Note that using an ArrayList to store a growable array of bytes is probably not a good idea, since each byte gets boxed, which means a new Byte object is allocated. So the total memory cost per byte is one pointer in the ArrayList + one Byte object. It's probably better to use a java.io.

ByteArrayOutputStream. There, the memory cost per byte is 1 byte. We can provide better advice if you describe the context a little more.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions