This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 2 years ago.
I have a method rotateImage that returns an int[][] array, I am having trouble understaing how to display the actual values of the array instead of just the hashcode:
[I#548c4f57
[I#1218025c
[I#816f27d
I tried doing this:
Arrays.stream(rotateImage(matrix)).forEach(System.out::println);
but that only returns the hashcode, not the actual numbers themselves. I also tried using to Arrays.toString() method in several ways but always get an error.
It's cause you're accessing the address and not the actual values. You can simply do a lambda function and iterate through each row
Arrays.stream(rotateImage(matrix)).
forEach(row -> {
for(int i : row) {
System.out.println(i);
}
});
Related
This question already has answers here:
Get only part of an Array in Java? [duplicate]
(8 answers)
Fastest way to get the first n elements of a List into an Array
(6 answers)
Closed 4 years ago.
I want to pick first N elements from a list in java
String[] ligature = new String{there,is not,a,single,person,who,knows,the,answer};
Now I want to pick first 5 elements from this.something like
Stiring[] selectedLigature = ligature[0:4];
I want to do it without using for loop.
Do not stream this simple case, there is subList for this:
// for a List
yourList.subList(0, 5)...
// for an array
Arrays.copyOfRange
Arrays class has a method for this:
Arrays.copyOfRange(ligature, 0, 5);
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:
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.
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.