i need java array to csharp [duplicate] - java

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[,]).

Related

How to create a generic array in Java [duplicate]

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);

"double[] array = {1,2,3,4};" vs "double[] array = new double[]{1,2,3,4,};" [duplicate]

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

Convert a string in code in Java [duplicate]

This question already has answers here:
Run piece of code contained in a String
(9 answers)
Closed 5 years ago.
I'm trying to convert a string in code in Java, but i have no idea how to do it or if it is possible.
This is my Java code (Measure is an other class I have created)
String str= "Measure m = new Measure(10,1);";
Is it possible to run the code in the string?
No I dont think that is a good practice, you don't need to do it that way,
just instantiate outside of the string, it will be fine and good practice.
Measure m = new Measure(10,1);

Java new Object[]{...} syntax [duplicate]

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};

What is the correct way to create a object array in java? [duplicate]

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.

Categories