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};
Related
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);
This question already has answers here:
How to initialize an array in Java?
(11 answers)
Closed 2 years ago.
what is the difference between these 2 array staments in java. In Statement 1 we dont use new keyword so my question is there object is created or not.
Statement 1: int[] arr = {10,20,30};
Statement 2: int[] arr = new int[]{10,20,30};
Both are the same, just a different way of writing:
In the first statement, the type is derived by the compiler in the array creation from the types / the declaration of arr.
In the second statement you have an explicit declaration of the instance. It contains more overhead.
This question already has answers here:
How can I create an instance of an arbitrary Array type at runtime?
(4 answers)
Closed 3 years ago.
i want to write this code( java ) on c# , but receive this error message
Unable to cast object of type 'System.SByte[,]' to type 'System.SByte[][]'.
java code is :
byte[][] bArr2 = (byte[][]) Array.newInstance(byte.class, new int[]{2, 8});
my csharp code is
sbyte[][] bArr2 = (sbyte[][])Array.CreateInstance(typeof(sbyte), new int[] { 2, 8 });
thanks
You already know the dimensions. So you should just declare the array normally.
Java:
byte[][] bArr2 = new byte[2][8];
C#:
sbyte[,] bArr2 = new sbyte[2,8];
In general, when porting between languages, I recommend to keep the standard language docs open so you can learn about the basic syntax like this (sbyte[,]).
This question already has answers here:
Declaring arrays without using the 'new' keyword in Java
(5 answers)
Closed 4 years ago.
I have been programming for quite sometime and I came across something I never noticed before.
What is the difference between these two?
double[] nums = {1,2,3,4};
double[] nums2 = new double[]{1,2,3,4,};
They both compile and have the same properties. At first I thought that nums2 could accept a new int[] and have integer values in it since its lower down in the hierarchy. But It actually didn't work.
The first way is just a shortcut syntax to create and initialize an array of the second way.
This is the only difference.
Take a look at https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 7 years ago.
I've seen examples such as:
Type arrayname[] = new Type[];
also as written as:
Type[] arrayname = new Type[]
I am quite confused about such expressions!
Where exactly should I put the []?
Any of the above are allowed. All produce the same bytecode. JLS-10.2 Array Variables says (in part)
The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.