Is the Braces in the following statement means an open array? - java

Integer[] ints = list.toArray(new Integer[]{});
If I remove "{}" the compiler asks to fill in a dimension for the array. What do the two braces mean as a command?

It means you initialize the array with what is in between the braces. Ex:
new Integer[] { 1, 2, 3}
Makes an array with 1, 2 and 3. On the other hand:
new Integer[] {}
Just mean that you initialize an array without any values. So it is the same as new Integer[0].

This actually means empty array. The {} allow you to supply the elements of the array:
Integer[] ints = list.toArray(new Integer[]{1, 2, 3});
is equivalent to:
Integer[] ints = new Integer[3];
ints[0] = 1;
ints[1] = 2;
ints[2] = 3;
Check this link:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
for more information - go to Creating, Initializing, and Accessing an Array section.

Yes, an empty array. Just like Integer[0].

Related

Fast conversion of single dimension array to two dimensional 'column' array

Are there any built-in functions (or fast implementations) in Java to convert a single-dimension array of size [n] to a two-dimensional array of size [n][1]?
For example:
double[] x = new double[]{1, 2, 3};
double[][] x2D = new double[][]{{1}, {2}, {3}};
Arrays.setAll method can be used:
double[] x = {1, 2, 3};
double[][] x2D = new double[x.length][];
Arrays.setAll(x2D, i -> new double[]{x[i]});
If you're looking for a built-in function to convert a single dimension array to a two-dimensional one, sadly there isn't one.
However, as it has been pointed out in the comments, you could use streams to reach a fair compact way to initialize your two-dimensional array from a single-dimension one. Basically, you could define your matrix's dimension from the array's length and then rely on a stream to copy the array's elements within the two-dimensional array.
double[] x = new double[]{1, 2, 3};
double[][] x2D = new double[x.length][1];
IntStream.range(0, x.length).forEach(i -> x2D[i][0] = x[i]);
Or in a more concise writing, you could immediately initialize your two-dimensional array with a stream where each element is mapped to a double[] and then collect each one of them into a further array.
double[] x = new double[]{1, 2, 3};
double[][] mat = Arrays.stream(x).mapToObj(i -> new double[]{i}).toArray(double[][]::new);

What is the most efficient way to initialize an array with values? [duplicate]

I know how to do it normally, but I could swear that you could fill out out like a[0] = {0,0,0,0}; How do you do it that way? I did try Google, but I didn't get anything helpful.
Check out the Arrays.fill methods.
int[] array = new int[4];
Arrays.fill(array, 1); // [1, 1, 1, 1]
You can also do it as part of the declaration:
int[] a = new int[] {0, 0, 0, 0};
Arrays.fill(). The method is overloaded for different data types, and there is even a variation that fills only a specified range of indices.
In Java-8 you can use IntStream to produce a stream of numbers that you want to repeat, and then convert it to array. This approach produces an expression suitable for use in an initializer:
int[] data = IntStream.generate(() -> value).limit(size).toArray();
Above, size and value are expressions that produce the number of items that you want tot repeat and the value being repeated.
Demo.
Arrays.fill(arrayName,value);
in java
int arrnum[] ={5,6,9,2,10};
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Arrays.fill(arrnum,0);
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Output
5 6 9 2 10
0 0 0 0 0
An array can be initialized by using the new Object {} syntax.
For example, an array of String can be declared by either:
String[] s = new String[] {"One", "Two", "Three"};
String[] s2 = {"One", "Two", "Three"};
Primitives can also be similarly initialized either by:
int[] i = new int[] {1, 2, 3};
int[] i2 = {1, 2, 3};
Or an array of some Object:
Point[] p = new Point[] {new Point(1, 1), new Point(2, 2)};
All the details about arrays in Java is written out in Chapter 10: Arrays in The Java Language Specifications, Third Edition.
Array elements in Java are initialized to default values when created. For numbers this means they are initialized to 0, for references they are null and for booleans they are false.
To fill the array with something else you can use Arrays.fill() or as part of the declaration
int[] a = new int[] {0, 0, 0, 0};
There are no shortcuts in Java to fill arrays with arithmetic series as in some scripting languages.
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Java how to get 1D array reference from a 2D array

Say I have a 2D array:
int[][] foo = new int[3][10];
I want to have a better alias name for each 1D array, like:
int[] xAxis = foo[0];
int[] yAxis = foo[1];
int[] zAxis = foo[2];
Is xAxis a reference to the 1st 1D array in 2D array? Cause I don't want the array to be copied again. If not, how do I get the reference to 1D arrays.
http://www.functionx.com/java/Lesson22.htm
...with one-dimensional arrays, when passing an multi-dimensional
array as argument, the array is treated as a reference. This makes it
possible for the method to modify the array and return it changed
when you declare this
new int[3][10]
you got one object array with 3 values: array1, array2, array3. All arrays will share the same defined size (10)
so, yes! in this case, foo[0] -> array1[10] , foo[1] -> array2[10] and foo[2] -> array3[10]
consider this one: what did you expect if foo[0] didn't pointing to another array? how (foo[x])[y] should works?
Yes it is reference to 1st Array in 2nd Array. You can verify it by yourself by modifing the xAxis array and check if reflects in 1st 1D array of 2D array.
Short answer - yes. The statement int[] xAxis = foo[0]; assigns the 1D array in foo[0] to the variable xAxis. Since arrays in Java are objects, this just assigns a reference. The data isn't being copied again.
Yes, array's are not primitive types, instead they are reference types.
So in your case xAxis will be a reference to foo[0]. Which means changes will be visible in both foo[0] and xAxis.
But you need to change your first statement to,
int[][] foo = new int[3][10];
I just tried out this code.
int[][] foo = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[] xAxis = foo[0];
int[] yAxis = foo[1];
int[] zAxis = foo[2];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
foo[i][j]++;
}
}
for(int i=0;i<3;i++){
System.out.println("X: " + xAxis[i] + " Y: " + yAxis[i] + " Z: " + zAxis[i]);
}
And yes, it indeed does reference it.

Is there any way of creating an array filled with a particular value in java? [duplicate]

I know how to do it normally, but I could swear that you could fill out out like a[0] = {0,0,0,0}; How do you do it that way? I did try Google, but I didn't get anything helpful.
Check out the Arrays.fill methods.
int[] array = new int[4];
Arrays.fill(array, 1); // [1, 1, 1, 1]
You can also do it as part of the declaration:
int[] a = new int[] {0, 0, 0, 0};
Arrays.fill(). The method is overloaded for different data types, and there is even a variation that fills only a specified range of indices.
In Java-8 you can use IntStream to produce a stream of numbers that you want to repeat, and then convert it to array. This approach produces an expression suitable for use in an initializer:
int[] data = IntStream.generate(() -> value).limit(size).toArray();
Above, size and value are expressions that produce the number of items that you want tot repeat and the value being repeated.
Demo.
Arrays.fill(arrayName,value);
in java
int arrnum[] ={5,6,9,2,10};
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Arrays.fill(arrnum,0);
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Output
5 6 9 2 10
0 0 0 0 0
An array can be initialized by using the new Object {} syntax.
For example, an array of String can be declared by either:
String[] s = new String[] {"One", "Two", "Three"};
String[] s2 = {"One", "Two", "Three"};
Primitives can also be similarly initialized either by:
int[] i = new int[] {1, 2, 3};
int[] i2 = {1, 2, 3};
Or an array of some Object:
Point[] p = new Point[] {new Point(1, 1), new Point(2, 2)};
All the details about arrays in Java is written out in Chapter 10: Arrays in The Java Language Specifications, Third Edition.
Array elements in Java are initialized to default values when created. For numbers this means they are initialized to 0, for references they are null and for booleans they are false.
To fill the array with something else you can use Arrays.fill() or as part of the declaration
int[] a = new int[] {0, 0, 0, 0};
There are no shortcuts in Java to fill arrays with arithmetic series as in some scripting languages.
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

initializing a 2d array in JAVA

If I declare a 2d array, for example:
int[][] numbers = new int[5][];
I thought that you had to individually declare/initialize each of the 5 int[]?
For example, before I assign a value to numbers[0][1], I would have to say:
numbers[0] = new int[4];
I wrote a small program and explicitly put a value in numbers[0][1], ran it, and it worked without initializing numbers[0] first.
Am I completely off thinking that the individual arrays have to be initialized first in a 2d array?
Edit: My misunderstanding was in the initialization. The 1st 2 statements are ok, because I declared the length of each int[] in goodArray to be 3, which causes all of them to be initialized. Whereas in the badArray declaration, I only declared how many arrays there were(3), so I get a npe:
int [][]goodArray = new int[3][3];
goodArray[0][1] = 2;
int[][] badArray = new int[3][];
badArray[0][0] = 2;
With multidimensional arrays in Java, you can specify a position, there is no need to individually define them.
You can easily test the behavior of the 2D arrays using examples like the one below:
int[][] numbers1 = new int[][] { {1, 2, 3}, {1, 2, 3, 4, 5}}; /* full initialization */
numbers1 = new int[3][]; /* [3][0] is null by default */
try {
System.out.println(numbers1[0][0]);
} catch (NullPointerException e) {
System.out.println(e);
}
numbers1[0] = new int[3]; /* all three ints are initialized to zero by default */
System.out.println(numbers1[0][0]);
numbers1[0] = new int[] {1, 2, 3};
System.out.println(numbers1[0][0]);
Will produce the following output:
java.lang.NullPointerException
0
1
int[][] numbers = new int[5][];
With the previous line you have created 5 int[] one dimensional array. Now you need to say the size for each one dimension array before you use them. Java gives this flexibility ao that you can have variable size one dimensional int [] arrays inside 2D array.

Categories