Declaring an array of int in xtend - arrays

How can I declare an array of int in xtend?
I've tried ArrayList but I get the error "The primitive 'int' cannot be a type argument".

Please refer to the docs for details, but essentially its something along these lines:
val int[] x = newIntArrayOfSize(5)
or if you want to define an array literal:
val int[] x = #[1, 2, 3]

Related

Creating a FloatArray from an Array<Float> object in Kotlin

I am trying to convert a Array object to a FloatArray using a spread operator:
val x = arrayOf(0.2f, 0.3f)
val y = floatArrayOf(*x)
Unfortunetly I get Type mismatch: inferred type is Array<Float> but FloatArray was expected
Why is there an error and how to make this work?
You cannot write that but instead you can do:
val y = x.toFloatArray()
toFloatArray is the obvious choice, but if for some reason you wanted to create a new float array without calling that, you could do the same thing it does internally: call the FloatArray constructor:
val x = arrayOf(0.2f, 0.3f)
val y = FloatArray(x.size) { x[it] }

Array with integers and nil values in golang?

I am new to golang. But I couldn't find an answer to this.
In python I can do,
array = [1, 2, 3, None, 5]
But in go when I write
var array = [5]int {1, 2, 3, nil, 5}
The compiler gives me the following error:
cannot convert nil to type int
How can I create an array in golang which is a mix of integer and nil values?
First a bit of theory. Python is a dynamically typed language. While each individual value has a type, your variables can be assigned values of any type without a problem. As a consequence, python collections can contain multiple types of values and don't complain. The following is a valid python tuple (1, 'a', None, True). Go is a statically typed language. If your variable is defined as an integer you cannot assign any non-integer value to it. As a consequence, collections in Go have a type and can only contain a single type of object.
Now to practice. There are a few ways to do what you want. The classical C way would be to pick an integer value you are never going to encounter and use that as a null value. A 0 or a -1 or something. This is not very robust though.
A more idiomatic way is to define your own type.
package main
import "fmt"
type NilInt struct {
value int
null bool
}
func (n *NilInt) Value() interface{} {
if n.null {
return nil
}
return n.value
}
func NewInt(x int) NilInt {
return NilInt{x, false}
}
func NewNil() NilInt {
return NilInt{0, true}
}
func main() {
var x = []NilInt{NewNil(), NewInt(10), NewNil(), NewInt(5)}
for _, i := range x {
fmt.Printf("%v ", i.Value())
}
}
Every type in go has a "zero value" which it is assigned when it is created to make sure it is always initialized. For int the "zero value" is 0, for strings "". There are some things that can have nil as their value though:
pointers
interfaces
maps
slices
channels
function types
Only the first is really relevant here though. If you want an int that is nil you have to declare it as a pointer to that int. As slices can't have mixed types you then need to make the slice a slice of type pointer to int: *int. So you could have a slice of pointers to ints that included a nil value:
zero := 0
ints := []*int{&zero, nil}
for _, anInt := range ints {
if anInt != nil {
fmt.Println(*anInt)
} else {
fmt.Println(anInt)
}
}
The *anInt in the println dereferences the pointer turning it into an int rather than a pointer to an int.
https://play.golang.org/

Variable character inside a constant

Having defined this:
int var1 = 1;
int var2 = 2;
int var3 = 3;
I want to make this:
int result = varc * 70; // Where c is a previously defined int that can take 1,2 or 3 value.
Solutions? Thank you.
In C you're out of luck on this since it's not a reflective language. That is you can't get the value of a variable by somehow "stringifying" the name you gave it in the source code.
But what you could do is use an array:
int vars[] = {1, 2, 3};
int result = vars[i] * 70;
where i is 0, 1, or 2.
you write:
int result = varc * 70;
This is what you want to make is not possible in language c.
Note: varc is an identifier
Remember IDENTIFIER in C :
Identifiers are names for entities in a C program, such as variables, arrays, functions, structures, unions.
It must be unique for all entities and also an identifier is a string of alphanumeric characters
Ok, you remembered. :)
So, you never used "c" present in "varc" to treat(refer) to other variables/identifies/entities.
I hope I might be solve your doubt in easiest way .Thank you! :)

Multiple ways to initialize arrays in c programming

In how many ways we can declare an array in C programming? if there are many ways to declare an array in C, what are the best practices or best way among?
So far I have been initializing an array like this:
int myArray[SIZE] = {1,2,3,4....};
What are the other ways do the same?
From C99, you can also use explicit indexes, called designators, in the initializer expression, which is sometimes very nice:
const int threetoone[] = { [2] = 1, [1] = 2, [0] = 3 };
The above is the same as
const int threetwoone[] = { 3, 2, 1 };
datatype arrayName[arraySize];
int x[10];
int x[]={1,2,3,4,5,6,7,8,9,0};
you can look at this question for more details on ways to initialize an array in C
declaring-and-initializing-arrays-in-c

Initializing multidimensional arrays in C

I'm just a tad confused about initializing multidimensional arrays in C...
This works:
int foo[2][MAX] = {
{2,4,34,43,23,0},
{2,4,34,43,23,0}
};
But this does not:
int foo[2][MAX];
foo = {
{2,4,34,43,23,0},
{2,4,34,43,23,0}
};
How come?
This syntax is for initialization, but in the second case you are using it for assignment which will not work.
type varName = someValue; // this is declaration and initialization
type varName; // this is declaration
varName = someValue; // this is assignment and not initialization
That is initialization can only be done at the declaration time, else it's a normal assignment.
The { syntax is only valid when you're initializing and declaring an array at the same time.
After declaring it, you need to use the complete syntax:
foo[0][0] = 2;
Technically speaking, C only has one-dimensional arrays. You create multidemnsional arrays by making arrays of arrays. The name of an array is converted to a pointer to its first element, and only the outer array is converted to a pointer. It's a pointer to an array of MAX ints, or int(*)[MAX].
As you said, this syntax is used to initialize an array, but in your second piece of code:
int foo[2][MAX];
Here, foo is uninitialized, and then
foo = {
{2,4,34,43,23,0},
{2,4,34,43,23,0}
};
This is assignment, not initialization.
Multidimensional or not, arrays in C are not copyable, which means that there's no way to assign anything to the entire array using core language features after the initialization is complete.
However, it is still possible to copy arrays by using memcpy function. In combination with compound literals it allows you to do what you tried to do. You just have to use a different syntax
int foo[2][MAX];
memcpy(
foo,
(int[2][MAX]) {
{2,4,34,43,23,0},
{2,4,34,43,23,0}
},
sizeof foo);
In C99 (or in gcc as an extension) you can make use of compound literals:
int (*foo)[MAX];
foo = (int [][MAX]) {
{2,4,34,43,23,0},
{2,4,34,43,23,0}
};
But note that foo must be declared as a pointer to MAX int's (not as a 2D array)
You have misunderstood the concept of 'initialization' and 'assignment`.
For example
int a = 10; // Initialization
Initialization is nothing but declaration + assignment.
But
int a; // Declaration
a = 10; // Assignment
When you do like this-
int foo[2][MAX] = {
{2,4,34,43,23,0},
{2,4,34,43,23,0}
};
internally it came to know 2 rows and MAX columns. It will initialize the whole array.
But-
int foo[2][MAX];
foo = {
{2,4,34,43,23,0},
{2,4,34,43,23,0}
};
Here foo represent the starting address of the array. Here you are trying to assign the values to 2D array. when you are trying to assign values to array it doesn't know how many rows and how many columns are there. so it is not possible and not allowed.
When you want to assign the input to array A better solution is scan it from user/ at run time-
int foo[2][MAX],i,j;
for(i=0;i<2;i++){
for(j=0;j<max;j++)
scanf("%d",&foo[i][j]);
}

Resources