Multidimensional array in java [duplicate] - java

This question already has an answer here:
Arrays with trailing commas inside an array initializer in Java
(1 answer)
Closed 7 years ago.
int [][] a = {{1,2,},{3,4}};
int [][] a = {{1,2},{3,4}};
i have two Multidimensional arrays in java. I want to know what's the exact difference in both of them.just see there is a comma after 2 in first one.

JLS 10.6. Array Initializers: A trailing comma may appear after the last expression in an array initializer and is ignored.

There is no difference. The extra comma might be useful if you are formatting your elements one per line, so that every element appears the same.

Related

"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

Arrays.asList(int_array).indexOf(int_element) returns -1 even if element is present java [duplicate]

This question already has answers here:
Converting array to list in Java
(24 answers)
Closed 5 years ago.
I have taken an int[] as input. For searching the index of an integer in the array I used Arrays.asList(arr).indexOf(element) method. However, I am getting index as -1 even if the element is present in the array.
int[] is one object, so Arrays.asList(arr) puts one object in the list, you need to put values from int[] one by one

how to use toString in arrays [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 5 years ago.
how to use toString() method in arrays bigger than one dimensional for example
int[][][] array = new array[x][y][z];
print one dimensional
Ststem.out.print(array[a][b].toString());
print all dimensional
System.out.print(array.toString());
my console only show :
[I#15db9742
You have a multidimentional array so you need to do:
Arrays.deepToString(array);
or nest for loops and print the element at index i,j,k
It looks like more like Java :).
In any case, I'd create a function that iterates through the different levels and print each element of the array.

Printing Two Dimension Arrays [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 6 years ago.
I know this isn't a difficult issue but I have come to a complete stand still, I want to print a two dimension array with values already assigned for example:
int array1[][] = new int[1][1];
array1[0][0] = 10;
array1[0][1] = 20;
array1[1][0] = 30;
array1[1][1] = 40;
I just want to simply print the values and I really can't remember how to do it, I keep on doing this
System.out.println(Arrays.toString(array1))`;
but I know this is wrong, can you help?
You may want to look at the Arrays.deepToString method, which is similar to Arrays.toString except that if the nested objects are arrays, it will recursively (and correctly) convert them into strings as well.
Also, note that there's a slight typo in your code - you need to size the array as
int[][] array1 = new int[2][2];
since you want a 2x2 array, not a 1x1 array.

How does Java calculate length of primitive array? [duplicate]

This question already has answers here:
Where is array's length property defined?
(7 answers)
Closed 8 years ago.
Code:
char[] chars = "abcd".toCharArray();
System.out.println(chars.length);
Question: How is length calculate by Java here? Since char is not a Class, I am not sure where length is stored. If it isn't stored, is it calculated every time you do chars.length? (I presume not)
The thing you wrote as char[] is an Object, an array, and has a public final field called length. It is calculated once when the array in created. Like all objects it also has a toString(), notify(), etc...
http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7
The public final field length, which contains the number of components
of the array. length may be positive or zero.
In this case chars.length is returning the number of elements in the array.
Or am I missing the question?
Even though char is not a Class/Object type in Java, an array of char actually is. And the length is a (final) field of that class. See the answer here for more.

Categories