how to use toString in arrays [duplicate] - java

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.

Related

Print out int[][] array using streams [duplicate]

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);
}
});

pick first N elements from a static list java [duplicate]

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);

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 to call on to alphabetize objects in an ArrayList? [duplicate]

This question already has answers here:
How can I sort an ArrayList of Strings in Java?
(5 answers)
Closed 7 years ago.
I'm trying to write a method that receives an ArrayList of Strings and puts it into alphabetical order. Is it possible to use the sort method for strings and not ints?
This is different from the other question because the way he did it is much more complicated than I'm aiming for. I just wanted a line of code rather than a whole block.
I am not completely sure what are you trying to say by
not int s
But if you want to sort A arraylist of string alphabetically, this can be a way.
java.util.Collections.sort(arrayListOfString);

Dont want the 1st position in array [duplicate]

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.

Categories