This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 6 years ago.
What does the below line of code mean?
is it a legal way to declare or define an array.
I googled it but couldn't find any information regarding this.
int[] []x[]
This is a valid line of code. It references a 3 dimensional integer array. Following codes are equivalent:
int[][][]x
int[][]x[]
int[]x[][]
int x[][][]
All of them are reference to a 3 dimensional integer array. You can declare a 10x10x10 size array like the following:
x = new int[10][10][10];
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:
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
This question already has answers here:
Java arrays printing out weird numbers and text [duplicate]
(10 answers)
Closed 8 years ago.
Java:
int [] list = new int [7];
System.out.print(list);
In the console it prints: [I#1f96302
Not only is it giving values other than ints, but it is giving more than I asked it too.
[l#1f96302 is the default way an Object is printed (that's what Object's toString() method returns for arrays). Try System.out.print(Arrays.toString(list)) instead, which will display the elements of the array.
This question already has answers here:
How to create a sub array from another array in Java?
(8 answers)
Closed 8 years ago.
I have one array, say
String[] a={a,b,c,d,e,f,g}
I want this array in second array but without the first element in the array, for example
String[] b{b,c,d,e,f,g}
How do I achieve this?
You can use System.arrayCopy() (jdk 1.5) or Arrays.copyOfRange() (jdk 1.6+) methods.