First question. I need an array of 2 ArrayLists, each ArrayList contains bytes. Something like
This won't compile, don't try. Ideas on how else to do it? The code using them will be something like:
See this to see why.
So, ideas for other ways to do this?
Second question, and I have a sneaking suspicion I'm not gonna understand the answer. Seems I can't create Arrays of Paramterized Types. Why not? Seems like a perfectly cromulent thing to do, given I really like the <type> notation I learned about a couple days ago. ArrayList<byte> is an object. I'm supposed to love and support objects. Things I love and support are not supposed to bite me in the butt when I most need them.
1) Yeah, I can have 2 for loops, one for foo and one for foo. Clumsy. Sad.
2) I understand what the link to 'why I can't create arrays of parameterized types' is telling me. What I don't understand is the why behind it. As I've never learned anything of compilers past "man gcc", I suspect I won't understand the answer.
What does this mean? Covariance means that you can assign a collection or array of elements of one type to a collection or array of elements of a less specific type. Here's an example:
You can not do this with Collection types by default:
Generics was introduced to improve type safety. Mixing them with arrays breaks that type safety:
Even though we properly parameterized all our generic types, we were still able to get a ClassCastException, something that generics were intended to prevent. That's because we were able to create a covariant array of an invariant collection.
The designers could have made this a warning instead of an error, but what would be the point? When you create an array of a parameterized type, you're saying: "I want type safety and I don't want type safety". That doesn't make sense. If you really want to do this and get a warning instead, you can just use raw types:
Jim Venolia wrote:. . .This won't compile . . .
There is a difference: B/b. You cannot make Lists of primitives, but must box them to the corresponding wrapper objects.
Piet Souris wrote:. . .
Go through the Java™ Tutorials and look for the sections about generics: there are two: This is the second. It has to do with arrays being covariant and generics being invariant, but I haven't got enough time to explain any more now.
Arrays are programmed into the javac tool on the assumption that programmers know what they are doing and always program code correctly. Try the following code which demonstrates that 3 is boxed into an Integer and that an Integer is a subtype of Number and that an Integer array can be assigned to a Number reference.Try running that code: no imports necessary.
You can see in line 1 that the three ints are boxed into Integers; you can see in line 3 that an Integer (again using boxing) can be assigned to a Number reference and line 5 shows that the compiler will permit me to insert the double 123.45 into the array because it is a Number (when boxed). Goodbye type‑safety. Well actually, before generics, that sort of thing was never type‑safe in the first place.
Now, we all know that a List<Integer> ISN'tT‑A List<Number> and is not a subtype of it. But List<Integer> is a subtype of List<? extends Number>. We also know that the intention of generics is to make code type‑safe. We also know that the post‑Java5 compiler is programmed on the basis of Murphy's Law:-
Now, let's imagine you have four Lists:-Output:-
Whatever can go wring, will go wrong.
Now, what if you wrote this:-Since a List<Integer> and a List<Double> are both subtypes of List<? extends Number>, what is stopping you from assigning lists to list4? Isn't is obvious that this will breach type‑safety? Since the idea behind generics is a higher level of type‑safety than existed in JDK1.4.2, the javac tool is programmed to refuse that sort of assignment, by making it impossible to create an array of any type with <> in its name:-
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [1.23, 4.56, 7.89]]
I did order the OCA/OCP Java SE Programmer Study guide from Amazon and it should arrive today. I suspect that will truly
An array/list/whatever of ArrayType<byte> would be very useful, but I suspect my time is better spent coming up with another way.
Thanks for the replies.
Practice, practice, practice. As you write things, and show us them, you will gradually come to understand them.
Jim Venolia wrote:Yeah, I knew I wouldn't understand the answer Too many of these concepts are new to me in JavaLand . . .
There are several answers to that:-
An array/list/whatever of ArrayType<byte> would be very useful . . .
That's a pleasure
Thanks for the replies.
Does my example (click) make more sense now?
The final result? I realized I could convert my byte into a string, then use substring() to make my 2 arrays. Have yet to see if that's a viable solution, and am tempted to ask "can my string contain any 8 bit value 0-0xff as long as I don't try to print it, or am I leaning on a wall waiting for a blindfold and cigarette?". Near as I can see a char is a 16 bit viable unicode doohicky, as I live in the USA learning unicode is akin to learning Mandarin Chinese.
My problem is here.
Transposition is a fairly standard matrix operation. I suggest you write a ByteMatrix class that is internally represented by a byte and has a height() property, a getRow(int index) method, a transpose() method, and a read(int width, InputStream bytes) factory method. The transpose() method returns another ByteMatrix, while the getRow() method returns a ByteBlock that simply wraps around a byte. You can add a getDistance(ByteBlock other) method to the ByteBlock class that calculates the distance between two blocks.
No. StringBuilder isn't a parametrised class, so there should be no problem putting its objects into an array.
Jim Venolia wrote:Can I assume the above thread is also why I can't create an array of StringBuilders? . . .
Why you would want a StringBulder, now that is a different question.