This question already has answers here:
How to create a generic array in Java?
(32 answers)
Closed 1 year ago.
class car<T>{
car<T>[] name;
name=(car<T>[]) new Object[5];
}
I tried to create an array of the class but the declare seem wrong. how should I set length of the array to 5 here?
Use java.lang.reflect.Array#newInstance:
car<T>[] name = (car<T>[]) java.lang.reflect.Array.newInstance(car.class, 5);
Related
This question already has answers here:
Converting 'ArrayList<String> to 'String[]' in Java
(17 answers)
Closed 3 years ago.
I am trying to initialize an array object with a get method which returns an arraylist object. I have tried using .toArray() to convert but it didn't work.
Would Project[] projects = list.toArray(new Project[[0]]) work? The reason it doesn't work normally is because by default toArray returns an Object[], and the JVM is unable to cast that to a Project[]. Passing in the project array allows it to determine the type of the desired array.
This question already has answers here:
Why isn't there a java.lang.Array class? If a java array is an Object, shouldn't it extend Object?
(4 answers)
Closed 3 years ago.
I would like to know what is the base class for byte array (byte[]) in java. For example for String it is java.lang.String.
The answer is byte[].class.
Example:
byte[] receivedMessage = consumer.receiveBody(byte[].class, 15000); // in ms or 15 seconds
This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 6 years ago.
What does the below line of code mean?
is it a legal way to declare or define an array.
I googled it but couldn't find any information regarding this.
int[] []x[]
This is a valid line of code. It references a 3 dimensional integer array. Following codes are equivalent:
int[][][]x
int[][]x[]
int[]x[][]
int x[][][]
All of them are reference to a 3 dimensional integer array. You can declare a 10x10x10 size array like the following:
x = new int[10][10][10];
This question already has answers here:
Creating an array of objects in Java
(9 answers)
Closed 7 years ago.
I don't recognize this kind of form Java syntax for constructor - granted I do very little Java now (taken from there):
new PersistentArrayMap(new Object[]{formKey, form});
I was expecting something along the lines of new Object(...) as parameter. Could someone decompose the above line ?
You are creating new Object array Object[] initialised to {obj1, obj2}
It is a way of declaring and initializing an array in java. A simpler example is the following:
int[] myIntArray = new int[]{1,2,3};
This question already has answers here:
Java arrays printing out weird numbers and text [duplicate]
(10 answers)
Closed 8 years ago.
Java:
int [] list = new int [7];
System.out.print(list);
In the console it prints: [I#1f96302
Not only is it giving values other than ints, but it is giving more than I asked it too.
[l#1f96302 is the default way an Object is printed (that's what Object's toString() method returns for arrays). Try System.out.print(Arrays.toString(list)) instead, which will display the elements of the array.