This question already has answers here:
Benefits of arrays
(9 answers)
Closed 8 years ago.
I see besides the ability to increase the capacity, the other difference between an array and arraylist is that the latter does not allow the base type as primitive types. But since we have unboxing, which converts, saying, an Int type to Integer type, now arraylist can store any type. So why do we still need arrays?
Arrays are more efficient, because they have fixed size.
Related
This question already has answers here:
Is an array an object in Java?
(14 answers)
Is an array a primitive type or an object (or something else entirely)?
(7 answers)
Closed 2 months ago.
In Java inside the angle brackets <> for collections we are supposed to provide the generics like Integer, String, Character etc and we can't give primitives but this is a correct list/Collection initialization:
List<int[]> ls = new ArrayList<>();
An array of primitives can be defined as generic? Because as far as I know in Java <> are used to define generics. When I first saw that kind of initialization I was surprised and curious and didn't believe it.
While int is a primitive type, its array type int[] is a reference type. Instances of int[] are objects just as much as instances of String. Generics may take reference types as their type arguments, but not primitive types. So List<int> is not allowed, but List<int[]> is, because int[] is a reference type.
You probably got confused because you internalized a syntactic rule like "can't have lower-case identifiers inside the pointy brackets", which is a good approximation for the rule that "type arguments cannot be primitive types", but arrays of primitives are not primitives.
This question already has answers here:
What does it mean that Java arrays are homogeneous, but ArrayLists are not?
(2 answers)
Closed 5 years ago.
We say Array in Java is homogeneous. Still, we can store heterogeneous data by declaring the array as an Object as below:
Object[] elements = new Object[10];
Then, how can we say Array is homogeneous? Can any one help me understand? Thanks in advance!
Array elements are homogeneous only at compile time; at run-time they may be heterogeneous.
An array is homogeneous only in terms of its elements' static, i.e. compile-time, type: all elements of the array are known to the compiler to have the same type.
As far as run-time type is concerned, however, any subtype of array's element type can be added to the array, opening a possibility for heterogeneous arrays at run-time.
This question already has answers here:
Java's final vs. C++'s const
(11 answers)
Closed 6 years ago.
I know that Java uses "final" to declare a constant and that c uses "const". Just wondering what the differences are between the two.
In java, making something final means that it can't be reasigned to another reference to another instance, but if it's a reference to a mutable class, the mutable values inside the class can still be modified.
For example, a final String is a constant because Strings are immutable in Java, but a final ArrayList means that you cannot assign it to another ArrayList, but you can still add and remove elements to that ArrayList
This question already has answers here:
What's the best way to get a Class object for an array type?
(2 answers)
Closed 4 years ago.
Is there a way to write the Java class for String arrays (or arrays for another class, for that matter) as a literal without creating an array object? The only Java term I can think of that gives the value is new String[0].getClass() which is creating a pointless array of length 0.
(BTW: I know that's cheap and I could put that in a static final, but I am curious whether there is another way.)
Try like this:
Class<String[]> cls = String[].class;
String[].class should do for you.
This question already has answers here:
Sort ArrayList of custom Objects by property
(29 answers)
Closed 7 years ago.
Is There a java util method, or a short way to sort an Array of some type by an attribute of that type. I currently have an array of Choice type objects where each have a getText() method that returns the visual representation of the choice. I can make a long method that creates an array of the choices texts, sort them, get their ordered index and then order the choices by that index, but I surely think there is some kind of a shortcut.
Any Suggestions?
Collections.sort(list, new Comparator<Choice>(){
public int compare (Choise c1, Choice c2) {
return c1.getText().compareTo(c2.getText());
}
});
add check for null if necessary
you can move comparator to external class and use reflection to read custom field from any object. but this will make code less understandable