Java integer arrays and generic arrays are commonly used data structures in Java programming. An integer array is a specific type of array that can only store data of integer type. Generic arrays are a more flexible array type that can store any type of data. In Java, we can use integer arrays and generic arrays to implement various functions, such as sorting, search, insertion, deletion and other operations. This article will introduce the basic concepts and common operations of Java integer arrays and generic arrays to help readers better understand and apply these two array types.
The 2022 edition of "ap computer science a prep" from The Princeton Review contains sample questions using the following code:
public class Screen { private ArrayList<int>[] data; . . . }
Is there any java version where arraylist<int>
is a valid construct? My understanding is that in all cases it needs to be arraylist<integer>
. With java 17 and 21 (which is what is installed on my system), arraylist<int>
produces an unexpected type
error.
If we define it as arraylist<integer>[] data
, how do we instantiate it correctly? The obvious solution is data = new arraylist<integer>[10]
(assuming we want 10 arraylists), but since java does not support generic arrays, when creating a generic array will fail.
I'm trying to understand if there are errors in this book or if my understanding of java is completely lacking. Thanks!
No. no way. Generics cannot contain primitive types, and there isn't even a version of Java that can (If you could once, don't you think it would be a big deal, especially its removal? Java rarely breaks Backward compatibility!)
impossible. You cannotproperly instantiate it2. It doesn't make any sense to have an array of non-primitive types. List<List<Integer>>
is superior to List<Integer>[]
in every way. So, just make a list of lists.
A casual web search couldn't find the specific content of the book, nor did the link you provided (it's hard to find, no doubt, and I assume it's copyrighted and the author didn't want it) No need to pay can be easily accessed). The key thing I'm looking for is some context in that chapter that explains why this isn't possible, or it's a bad idea, or, well, anything that shows that the problem you're emphasizing is this This is the point this book is trying to express.
If this isn't in the book and they either claim it's valid java or they claim it's somehow relevant, then it's a crap book. Maybe that's why I can't find it online :)
in conclusion:
You cannot create a List<int>
. You can't do that now. You've never been able to do it. 1
List<Integer>[]
is an idiot. No code should be written like this. Arrays have various disadvantages. Their advantages are few; their advantages are irrelevant in this case, which is why arrays do exist (partly as low-level "glue" - the implementation of java.util.ArrayList
is Written in java, and uses Object[]
internally. Something has to - partly because int[]
is currently really smaller in space and time than List<Integer>
More efficient. Hence the stupid idea that you should never write, so any book that goes into depth on how to make it work is... weird. For someone who is 15 years into their Java career This should be a good lesson, if any. It's like teaching a race car driver about the transmission of the original Ford Model T. It's relevant to the car. Whatever lesson is learned from it, though, is completely Doesn't matter.
[1]Possiblyaround JDK25Possiblyable - Project Valhalla is the OpenJDK team that is trying to find a way to create codable data types similar to standard reference types, But the implementation and behavior are similar to primitives, and as part of that, List<int>
could probably be turned into a thing. This part in particular (trying to treat int
as just a value type variant of j.l.Integer
and make those types work in generics) is something the Valhalla project hasn't really solved yet, And it may never be resolved - there are significant backwards compatibility issues. For example how the j.u.List
interface has a method remove(int x)
which removes the object at index int x
, and a method remove(Object o)
, this method scans the list, comparing each object in the list with o
- once a match is found (item.equals(o)
is true
), that is, is deleted, and the remove
method returns. In addition to the following issues: remove()
, contains()
, etc. take Object
as parameter, not E
(since you can ask Whether the given pear is in the list of apples. The answer is very simple: no, but, it's not an illogical question, hence why this choice was made) - Trying to unify generics and primitives means that both signatures are now remove(int)
, this is obviously impossible. This is just the trickiest part of Valhalla and one of a million reasons why it may never happen. If you want to know more about this, or just stay up to date, check out JEP 218. This edit was brought about by a comment from @M.Justin :)
[2] "Appropriate" is a subjective term. I define it here exactly as I expect: zero warnings from the compiler, no
needed. Could also be defined as: without breaking the promise made by the array - which is also impossible (a "promise" is that the array can tell you its component type at runtime via arrRef.getClass().getComponentType( )
. Simply put, this only tells you: I am an array of ArrayList
references - it cannot tell you ArrayList<Integer>
. Only ArrayList
.
Is ArrayList<int> a valid construct? -
no. In Java, generic types do not work with primitive data types.
ArrayList<Integer>[] correctly? - You can achieve this by -
Java does not allow you to create a generic array
, but you can simply create an array of objects, in this case an ArrayList. While this will give you a warning about unchecked or unsafe operation, you can overcome this by passing the @SuppressWarnings annotation.
The above is the detailed content of Java integer arrays and generic arrays. For more information, please follow other related articles on the PHP Chinese website!